Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

www.webdp.net, Event 9 IIS log failed to write entry, wwwxxx jeffs, Catastrophic failure Unexpected method call sequence. 0x8000ffff (-2147418113)., ksh lock a file, [unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_DBC failed, sed: -e expression #1, char 1: unterminated address regex, procmail + change subject, w2ksp4.exe download, /proc/kallsyms format

Links

XODOX
Impressum

#1: how to globalize a lexical variable inside a sub routine

Posted on 2008-04-10 01:30:32 by sunnyIsland

------=_NextPart_000_0006_01C89ADC.C23D2990
Content-Type: text/plain;
charset="gb2312"
Content-Transfer-Encoding: quoted-printable

Hi,
Assuming my script use a strict pragma.=20
If I have a lexical variable declared inside a sub routine and I wish to =
globalize this lexical variable, what are the various options to =
globalise this variable?

Thanks

###############
use strict;
use warnings;

testing();

sub testing {
my $variable =3D 2;
print "I can see you Mr $variable\n";
}

print "So what are the various options to globalise this \$variable =
which is $variable\n";

------=_NextPart_000_0006_01C89ADC.C23D2990--

Report this message

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

Posted on 2008-04-10 02:08:56 by Jenda Krynicky

From: <itshardtogetone@hotmail.com>
To: <beginners@perl.org>
Subject: how to globalize a lexical variable inside a sub routine
Date sent: Thu, 10 Apr 2008 07:30:32 +0800

> Hi,
> Assuming my script use a strict pragma.
> If I have a lexical variable declared inside a sub routine and I wish
> to globalize this lexical variable, what are the various options to
> globalise this variable?
>
> Thanks
>
> ###############
> use strict;
> use warnings;
>
> testing();
>
> sub testing {
> my $variable = 2;
> print "I can see you Mr $variable\n";
> }
>
> print "So what are the various options to globalise this \$variable which is $variable\n";

What do you mean by "globalise"?

Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

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

Posted on 2008-04-10 02:40:03 by chas.owens

On Apr 9, 2008, at 19:30, <itshardtogetone@hotmail.com> <itshardtogetone@hotmail.com
> wrote:
> Hi,
> Assuming my script use a strict pragma.
> If I have a lexical variable declared inside a sub routine and I
> wish to globalize this lexical variable, what are the various
> options to globalise this variable?
>
> Thanks
>
> ###############
> use strict;
> use warnings;
>
> testing();
>
> sub testing {
> my $variable = 2;
> print "I can see you Mr $variable\n";
> }
>
> print "So what are the various options to globalise this \$variable
> which is $variable\n";

You don't. The testing function should return the value and the
caller should catch it in another lexical variable. Global variables
are rarely the right answer to a problem. What exactly are you trying
to solve with a global variable?

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

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

Posted on 2008-04-10 23:49:11 by sunnyIsland

Thanks Jenda, Chas.Owens and fellow members.

> What do you mean by "globalise"?
> Jenda

Looking at the script below, in diagram 1, $capital will not be incremented
unless I write it as in diagram 2.
Just because I do not want $capital to be reset on each foreach loop, I then
have to declare $capital as in diagram 2. This seems to defeat the purpose
of using the "use strict" pragma.
Thanks


#####diagram 1#########
use strict;
my $banker = 5;
my $player = 3;
my $stake = 1;

foreach (qw/1..10/){
if ($banker > $player){
my $capital += $stake;
}
}
#### diagram 2 ###########
use strict;
my $banker = 5;
my $player = 3;
my $stake = 1;
my $capital = 1;

foreach (qw/1..10/){
if ($banker > $player){
$capital += $stake;
}
}




--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

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

Posted on 2008-04-11 00:23:12 by Jenda Krynicky

Something like

....
{
my $cnt;
sub inc {
return ++$cnt;
}
}

print inc(),"\n";
print inc(),"\n";
print inc(),"\n";

Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

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

Posted on 2008-04-11 01:34:07 by krahnj

itshardtogetone@hotmail.com wrote:
> Thanks Jenda, Chas.Owens and fellow members.
>
>> What do you mean by "globalise"?
>> Jenda
>
> Looking at the script below, in diagram 1, $capital will not be
> incremented unless I write it as in diagram 2.
> Just because I do not want $capital to be reset on each foreach loop, I
> then have to declare $capital as in diagram 2. This seems to defeat the
> purpose of using the "use strict" pragma.
> Thanks
>
>
> #####diagram 1#########
> use strict;
> my $banker = 5;
> my $player = 3;
> my $stake = 1;
>
> foreach (qw/1..10/){

You have a list with one element, in other words:

foreach ( '1..10' ) {

If you really meant to generate the list 1,2,3,4,5,6,7,8,9,10 then you
have to do it like this:

foreach ( 1 .. 10 ) {

But instead of using a loop you could just do this:

my $capital = $stake * 10 if $banker > $player;


> if ($banker > $player){
> my $capital += $stake;
> }
> }
> #### diagram 2 ###########
> use strict;
> my $banker = 5;
> my $player = 3;
> my $stake = 1;
> my $capital = 1;
>
> foreach (qw/1..10/){
> if ($banker > $player){
> $capital += $stake;
> }
> }


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

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

Posted on 2008-04-11 13:55:40 by Peter Scott

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/


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message