Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
P is for Practical
 
PerlMonks  

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (12)
As of 2014-04-23 18:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (552 votes), past polls