Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: About "state" variables in Perl 5.10

by citromatik (Curate)
on Dec 28, 2007 at 20:22 UTC ( #659393=note: print w/replies, xml ) Need Help??


in reply to Re: About "state" variables in Perl 5.10
in thread About "state" variables in Perl 5.10

Note also that with state each created anonymous sub gets its own copy of $x

Hmmm... but you can get the same result with the closure approach:

sub get_counter { my $x = 42; return sub { $x++; } } my $s = get_counter(); say &$s();
However, that difference is not why we introduced state variables. The big win is the psychological one of not having to look outside of the sub to find the definition of $x.

Nice, that reason convince me (and the one that kyle gave below).

Thanks

citromatik

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (1)
As of 2021-05-10 01:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (103 votes). Check out past polls.

    Notices?