Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Re: Re: Unusual Closure Behaviour

by iakobski (Pilgrim)
on Jul 12, 2001 at 19:54 UTC ( #96081=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Unusual Closure Behaviour
in thread Unusual Closure Behaviour

HyperZonk and Synapse0 have both suggested that the my is being ignored and all that is happenning is that $x is simply global.

It's a good theory, and certainly what I thought when I first looked at it. But it's easy to see that this isn't true: try comparing

sub foo{ my $x if 0; print "FOO: ", $x++, "\n"; } sub bar{ my $x if 0; print "BAR: ", $x--, "\n"; } foo, bar for (1..10);
with
sub foo{ print "FOO: ", $x++, "\n"; } sub bar{ print "BAR: ", $x--, "\n"; } foo, bar for (1..10);

I reckon the real reason is the explanation given by Abigail.

Now I don't think I would ever write my $x if 0 unless I wanted to get the sack for writing obscure code, but how about:

sub STATIC_VARIABLE{ 0 } sub foo{ STATIC_VARIABLE && my $x; print "FOO: ", $x++, "\n"; }
would that be difficult to understand? Would it carry on working in the future?

-- iakobski

Replies are listed 'Best First'.
Re: Re: Re: Re: Unusual Closure Behaviour
by HyperZonk (Friar) on Jul 12, 2001 at 21:35 UTC
    Obscure stuff indeed. I think that Abigail was indeed correct. I suppose I should have actually tested my hypotheses in code, like you did. <blush>

    It seems that, despite what the perlref docs seem to say, the scoping of the variable is done at compile time, but the variable's reinitialization is done at run-time. Well, I guess we should expect at least the first since it is a lexical, right?

    In any case, because of the compile-time scoping, we end up with a local via my no matter if the line in which it is declared will ever run or not. But the initialization never runs because of the permanently false conditional at the point at which the run-time must run initialization code. Actually, I suspect that we should expect this behavior because data scoping is easily done at compile, but initialization must be done at run-time.

    Now, I think that this could be considered a feature for those savvy to this rather bizarre behavior. And I like the recommended usage from iakobski. But I would put a comment in at the STATIC_VARIABLE declaration, in case the script needs to be maintained by a non-monk.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://96081]
help
Chatterbox?
[james28909]: im not quite how to explain it any better nick. you evolved from ignorance to intelligence. not the other way. the universe evolves from gas coulds and debris into planets stars and galaxies ect. it doesnt happen any other way. hence it has ....
[james28909]: some kind of logic behind it
[james28909]: and that is also anothe rpoint i made, i think it has to do with perception of the world around you. most people think of evolution on a human scale. why could life evolve on this planet? because this planet evolved in this solar system. and so on.
[holli]: here's something for you to watch, James. I think you will like it
[erix]: for the record: I have not downvoted anyone on that subthread that was my fault
[james28909]: there are all kinds of things that had to happen to let life come to be. but at the same time, life may not be the end goal IF there is any kind of end goal lol
[james28909]: well who is the person who gets to decide which behaviour is worthy of a downvote? a person with their own beliefs? xD
[erix]: teleology -- I've never understood why that was thunk up
[erix]: ( and when teleology was brought up, during my biology-study., I couldn't get an answer either )
[1nickt]: FTR I agree with you about "design." Just not sure about the trajectory of evolution. THere are a lot of dead-ends in the evolutionary paths. I suspect homo sapiens is just one of them.

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (15)
As of 2017-12-15 14:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (433 votes). Check out past polls.

    Notices?