in reply to Re: Unforgettable Closures?
in thread Unforgettable Closures?

Oh boy, Occam's razor. Thanks, that clears it up, I guess. I've not got anything newer than Perl 5.8.8, and grepping perlsyn for the said gotcha finds nothing, so I'm assuming I need a newer Perl?

Replies are listed 'Best First'.
Re^3: Unforgettable Closures? (shave)
by tye (Sage) on Dec 10, 2007 at 03:46 UTC

    No, you don't need a newer version of Perl (it wouldn't help you, for several reasons1).

    You need to change your code that looks like:

    my $var= ... if ...;

    to instead look like

    my $var; $var= ... if ...;

    You don't want to skip the run-time effect "my" when your conditional is false. That run-time effect is what resets your variable so that you don't get a previous value.

    - tye        

    1 A) The newer version just adds warning; it doesn't change how anything works. B) The warning doesn't even apply to what you've done; it appears this new warning is pretty stupid, in fact. C) Just fix your code and it works fine on old versions of Perl.

      (shave)
      hehe. Yeah, Occams Razor because of my mistaken assumptions (closure funkiness versus perlsyn (my) funkiness). Me:
      Thanks, that clears it up, I guess. I've not got anything newer than Perl 5.8.8, and grepping perlsyn for the said gotcha finds nothing, so I'm assuming I need a newer Perl?
      I meant new version of Perl for which the man page for perlsyn mentions the "my" strangeness. I ought to have mentioned it, but before I'd reproduced the funkiness on my local 5.8.8 installation, I came across it on (sigh) 5.6.1 - on which the following fix worked:
      Well whatever the reason is, the fix was simply:
      #my $func = shift if ref $_[0] eq 'CODE'; my $func = ref $_[0] eq 'CODE' ? shift() : undef;
      Of course, as you rightly point out, separating the "my" and the conditional also works, like you point out.
      my $func; $func = shift if ref $_[0] eq 'CODE';
      all good! Thanks.

        I meant new version of Perl for which the man page for perlsyn mentions the "my" strangeness.

        The warning has been in perlsyn since 5.8.1.

        I've not got anything newer than Perl 5.8.8, and grepping perlsyn for the said gotcha finds nothing,

        It's there. Look at "NOTE:" at the end of the "Statement Modifiers" section.

        [doc://perlsyn] goes to perlsyn which currently claims to be 5.8.8 vintage and says:

        NOTE: The behaviour of a my statement modified with a statement modifier conditional or loop construct (e.g. my $x if ... ) is undefined. [etc.]

        In case that helps you in future searches or something. Perhaps the highlighting tripped up your grep or something? :)

        Update: DYK you can type "doc://perlsyn" in the 'Search' box found at the top of most PerlMonks pages and be taken to the same place as [doc://perlsyn] ? FYI. I use that feature all the time. [ For example, type "abbr://dyk" if that abbr. isn't familiar or type "abbr://fyi" to see a bunch of expansions that don't match what I meant (and one that did) :]

        - tye