Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re^2: Use of "my" after Perl v5.14

by 2teez (Priest)
on Sep 21, 2012 at 02:21 UTC ( #994798=note: print w/ replies, xml ) Need Help??

in reply to Re: Use of "my" after Perl v5.14
in thread Use of "my" after Perl v5.14

Had the declaration been "my $n = 0", then for every subroutine call it would start with 0...

Depending on where "my $n = 0" was placed and how.
Using "my $n = 0" outside the subrourtine, placed in a block like so:

for (1..5){marine()}; { my $n=0; # private, persistent variable $n sub marine { $n += 1; print "Hello, sailor number $n!\n"; } }
will work exactly, like using:
use 5.010; for (1..5){marine()}; sub marine { state $n = 0; # private, persistent variable $n $n += 1; print "Hello, sailor number $n!\n"; }

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me

Comment on Re^2: Use of "my" after Perl v5.14
Select or Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2015-11-25 23:19 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (693 votes), past polls