how to globalize a lexical variable inside a sub routine

how to globalize a lexical variable inside a sub routine

am 10.04.2008 01:30:32 von 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--

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

am 10.04.2008 02:08:56 von Jenda Krynicky

From:
To:
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/

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

am 10.04.2008 02:40:03 von chas.owens

On Apr 9, 2008, at 19:30, > 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/

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

am 10.04.2008 23:49:11 von 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/

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

am 11.04.2008 00:23:12 von 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/

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

am 11.04.2008 01:34:07 von 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/

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

am 11.04.2008 13:55:40 von 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/