Browse Groups

• what is the difference between \$x=12; sub abcd { \$x=0; print \$x; } and \$x=12; sub abcd { local \$x=0; print \$x; }
Message 1 of 4 , Feb 26, 2010
View Source
what is the difference between
\$x=12;
sub abcd
{
\$x=0;
print \$x;
}

and

\$x=12;
sub abcd
{
local \$x=0;
print \$x;
}
• In the first, x is a global variable; in the second, x has two instances -- the global, and the local inside the subroutine. Mind you, neither will output
Message 1 of 4 , Feb 26, 2010
View Source
In the first, x is a global variable; in the second, x has two
instances -- the global, and the local inside the subroutine. Mind
you, neither will output anything because you don't call the
subroutine in the example code given. Once modified to actually run,
both will give the same output. In the first case, you're changing
the global \$x to 0; in the second, you're creating a local \$x which is
set to zero.

munch> cat > foo.pl
#!/usr/bin/perl
\$x=12;
abcd();
sub abcd
{
\$x=0;
print \$x;
}
munch> ./foo.pl
0
munch> cat > foo.pl
#!/usr/bin/perl
\$x=12;
abcd();
sub abcd
{
local \$x=0;
print \$x;
}
munch> ./foo.pl
0
munch>

j

On 26 Feb 2010, at 7:28, Jagadesh wrote:

> what is the difference between
> \$x=12;
> sub abcd
> {
> \$x=0;
> print \$x;
> }
>
> and
>
> \$x=12;
> sub abcd
> {
> local \$x=0;
> print \$x;
> }
>
>
>

[Non-text portions of this message have been removed]
• Hey, John,...It s Mario (formerly with PRC) How are things going (assuming that this is the same JF)? Sent from my Verizon Wireless BlackBerry ... From: John
Message 1 of 4 , Feb 26, 2010
View Source
Hey, John,...It's Mario (formerly with PRC) How are things going (assuming that this is the same JF)?
Sent from my Verizon Wireless BlackBerry

-----Original Message-----
From: John Francini <francini@...>
Date: Fri, 26 Feb 2010 13:34:31
To: <perl-beginner@yahoogroups.com>
Subject: Re: [PBML] func

In the first, x is a global variable; in the second, x has two
instances -- the global, and the local inside the subroutine. Mind
you, neither will output anything because you don't call the
subroutine in the example code given. Once modified to actually run,
both will give the same output. In the first case, you're changing
the global \$x to 0; in the second, you're creating a local \$x which is
set to zero.

munch> cat > foo.pl
#!/usr/bin/perl
\$x=12;
abcd();
sub abcd
{
\$x=0;
print \$x;
}
munch> ./foo.pl
0
munch> cat > foo.pl
#!/usr/bin/perl
\$x=12;
abcd();
sub abcd
{
local \$x=0;
print \$x;
}
munch> ./foo.pl
0
munch>

j

On 26 Feb 2010, at 7:28, Jagadesh wrote:

> what is the difference between
> \$x=12;
> sub abcd
> {
> \$x=0;
> print \$x;
> }
>
> and
>
> \$x=12;
> sub abcd
> {
> local \$x=0;
> print \$x;
> }
>
>
>

[Non-text portions of this message have been removed]

------------------------------------

Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.htmlYahoo! Groups Links
• In one case you set the global x to 0 and if you call this subr. x the value of x will be changed. When you say local the local x will not be seen outside of
Message 1 of 4 , Feb 26, 2010
View Source
In one case you set the global x to 0 and if you call this subr. x
the value of x will be changed.
When you say local the local x will not be seen outside of the subr.
and won't change global x.
Instead of local it is better to use my \$x=0;

On Feb 26, 2010, at 7:28 AM, Jagadesh wrote:

> what is the difference between
> \$x=12;
> sub abcd
> {
> \$x=0;
> print \$x;
> }
>
> and
>
> \$x=12;
> sub abcd
> {
> local \$x=0;
> print \$x;
> }
>
>

[Non-text portions of this message have been removed]
Your message has been successfully submitted and would be delivered to recipients shortly.
• Changes have not been saved
Press OK to abandon changes or Cancel to continue editing
• Your browser is not supported
Kindly note that Groups does not support 7.0 or earlier versions of Internet Explorer. We recommend upgrading to the latest Internet Explorer, Google Chrome, or Firefox. If you are using IE 9 or later, make sure you turn off Compatibility View.