develooper Front page | perl.perl5.porters | Postings from August 2014

Re: [perl #122567] [PATCH] Warn when a conditional my() is used withan assignment

Thread Previous
Dave Mitchell
August 21, 2014 15:05
Re: [perl #122567] [PATCH] Warn when a conditional my() is used withan assignment
Message ID:
On Tue, Aug 19, 2014 at 01:52:59PM -0700, Lubomir Rintel wrote:
> The "Deprecated use of my() in false conditional" failed to trigger in case
> there was an assignment (my $x = 123 if 0), silently creating "static"
> variables without a warning. This aims to correct thit and adds a test case.
> The only occassion in perl tree that triggers the warning is in Math::BigFloat,

I'm in two minds about this. Note that the usage in Math::BigFloat *is*
actually useful:

    sub DEBUG () { 0; }

    my $steps = 0 if DEBUG;
    if (DEBUG) {
        $steps++; print "step $steps = $x\n" if $steps % 10 == 0;

With this, the whole 'my $steps = 0' statement is optimised away
at compile time if DEBUG is set to 0.  With your proposed patch to
Math::BigFloat, a useless 'my $steps = 0' is executed each time.

On the other hand for this particular case, optimising away that
statement is probably indetectable in terms of performance improvement.

Also, I added the original 'my $foo if 0' warning, and if Math::BigFloat
had been written as

    my $steps if DEBUG;
    $steps = 0 if DEBUG;

then it would have already warned under the existing regime, even though
the 'if DEBUG' is usefully optimising away an unneeded run-time op. So I'm
being a bit hypocritical here.

Possibly a compromise would be to only emit the warning if the /FOLD flag
is present on the constant?

So these would continue warning:

    my $x if 0;
    my $x if DEBUG;

this would start to warn:

    my $x = 0 if 0;

but this would continue not warning:

    my $x  = 0 if DEBUG;

The warp engines start playing up a bit, but seem to sort themselves out
after a while without any intervention from boy genius Wesley Crusher.
    -- Things That Never Happen in "Star Trek" #17

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About