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:
will work exactly, like using:for (1..5){marine()}; { my $n=0; # private, persistent variable $n sub marine { $n += 1; print "Hello, sailor number $n!\n"; } }
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
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me
In Section
Seekers of Perl Wisdom