Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Unusual Closure Behaviour

by paulbort (Hermit)
on Jul 12, 2001 at 21:08 UTC ( #96114=note: print w/ replies, xml ) Need Help??


in reply to Unusual Closure Behaviour

The word that I keep seeing left out of these replies is 'precedence'. It looks to me like there is confusion between my ($x if undef); and (my $x) if undef; It looks to me like you are expecting the latter and getting the former. I suspect 'if' is being evaluated as an operator, so it gets a higher precedence than the 'my' function, and confusion results. Or I could be completely wrong.


Comment on Re: Unusual Closure Behaviour
Re: Re: Unusual Closure Behaviour
by HyperZonk (Friar) on Jul 12, 2001 at 22:19 UTC
    Actually, the if being evaluated first is exactly what we would want in any case. You are missing the implication of the behavior, however. The variable, besides being localized which is unexpected (until one realizes that data scoping is done at compile-time in this case), also becomes static; that is, it retains its value between calls, which is unexpected behavior for a local via my variable. See iakobski's reply in this thread for more details.
Re: Re: Unusual Closure Behaviour
by tachyon (Chancellor) on Jul 13, 2001 at 06:57 UTC

    Hi I think this clarifies the situation. $x is definitely lexically scoped. Run this as in then uncomment the use strict.

    # use strict; sub foo { my $x if 0; return ++$x; } for (1..7){ print foo(); print "($x)"; };
    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://96114]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2014-12-21 18:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (106 votes), past polls