Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > Re: how to glob...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 7 Topic 10914 of 12261
Post > Topic >>

Re: how to globalize a lexical variable inside a sub routine

by Peter@[EMAIL PROTECTED] (Peter Scott) Apr 11, 2008 at 04:55 AM

On Fri, 11 Apr 2008 00:23:12 +0200, Jenda Krynicky wrote:
> Something like
> 
> ...
> {
> 	my $cnt;
> 	sub inc {
> 		return ++$cnt;
> 	}
> }
> 
> print inc(),"\n";
> print inc(),"\n";
> print inc(),"\n";

Just to put in a plug for the modern conveniences of Perl 5.10:

use 5.10.0;
sub inc {
    state $count;
    return ++$count;
}
say inc;
say inc;
say inc;

-- 
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/
 




 7 Posts in Topic:
how to globalize a lexical variable inside a sub routine
itshardtogetone@[EMAIL PR  2008-04-10 07:30:32 
Re: how to globalize a lexical variable inside a sub routine
Jenda@[EMAIL PROTECTED]   2008-04-10 02:08:56 
Re: how to globalize a lexical variable inside a sub routine
itshardtogetone@[EMAIL PR  2008-04-11 05:49:11 
Re: how to globalize a lexical variable inside a sub routine
Jenda@[EMAIL PROTECTED]   2008-04-11 00:23:12 
Re: how to globalize a lexical variable inside a sub routine
Peter@[EMAIL PROTECTED]   2008-04-11 04:55:40 
Re: how to globalize a lexical variable inside a sub routine
krahnj@[EMAIL PROTECTED]   2008-04-10 16:34:07 
Re: how to globalize a lexical variable inside a sub routine
chas.owens@[EMAIL PROTECT  2008-04-09 20:40:03 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Dec 3 17:48:38 CST 2008.