in reply to Curious result of using "my ... if ..."

It's a pretty well known bug. Perl 5.10 throws a warning for that type of construct.
  • Comment on Re: Curious result of using "my ... if ..."

Replies are listed 'Best First'.
Re^2: Curious result of using "my ... if ..."
by grinder (Bishop) on May 16, 2007 at 06:55 UTC
    5.10 throws a warning for that type of construct

    No it doesn't. I seem to recall that blead did, at some point, but it appears to no longer do so.

    It has changed the behaviour, now the assignment just becomes undefined, but the previous value is not recovered from the pad.

    #! /usr/local/bin/perl5.9.5 use strict; use warnings; a(10); a(0); a(20); sub a { my $num = shift; my $x = $num * 100 if $num; print $x, $/; } # produces # 1000 # Use of uninitialized value $x in print at ./myif line 13. # 2000

    Real state variables are now available with the state declarator:

    sub c { state $count = 10; return $count++; } print c(), $/; # 10 print c(), $/; # 11 print c(), $/; # 12

    • another intruder with the mooring in the heart of the Perl

      5.10 throws a warning for that type of construct

      No it doesn't. I seem to recall that blead did, at some point, but it appears to no longer do so.

      I constantly wonder why it is not an error at all. Whatever its behaviour could be in an actual implementation, I can see no logical nor intuitive semantics that could be attached to the construct. It has a meaning to do something upon a condition. How can have a meaning for a thingie to exist or not, upon a condition? Point is, declaration happens at compile time, condition is verified at runtime. "Ideally" one could expect that in statements following

      my $var=whatever if condition;

      $var would be the lexical one just declared if condition is true, and something else (as if the declaration just were not there) otherwise: clearly, any way you look at it, it's unsatisfactory.

        I don't see why this should be an error. Just as

        my $x = 5 if $t;
        could be semantically equivalent to
        my $x; $x = 5 if $t;
        then
        my $x if $t;
        could be semantically equivalent to
        my $x; $x if $t;
        If that throws a warning, then, fine. It certainly shouldn't be an error (unless, of course, you've asked that this type of warning be converted to an error).

        A word spoken in Mind will reach its own level, in the objective world, by its own weight
Re^2: Curious result of using "my ... if ..."
by GrandFather (Saint) on May 16, 2007 at 04:52 UTC

    Not so much a bug as an unmapped and(possibly) unmappable area of the Perl landscape. What is the sensible behaviour? Personally I think an error is appropriate.


    DWIM is Perl's answer to Gödel
      In my opinion, the sensible behavior, and what most people expect when they use it, is that it would operate the same as if the my($x) was on a separate line. If that isn't possible, it should throw an error.
        I agree with you. There are two logical choices: either it should work as you suggest or it should be disallowed by the compiler. But the actual implementation makes no sense whatsoever to me.