C
chacallot
Hi!
I though I understood the thing : when using a my $variable the scope is
the {} block.
I learned that the hard way while doing my first perl scripts and I
thought I understood it all thanks to perlintro "variable scoping"
(http://aspn.activestate.com/ASPN/docs/ActivePerl/5.8/lib/Pod/perlintro.html)
:
"The variables are scoped to the block (i.e. a bunch of statements
surrounded by curly-braces) in which they are defined."
So can someone give me a hint or a link to the rite (f) manual about how
to understand the result of the following example.
----------------
use strict;
for (my $i=1;$i<3;$i++)
{
my $toto=$i;
print "before sub toto=$toto\n";
titi();
print "after sub toto=$toto\n";
sub titi
{
$toto++;
print "in sub toto=$toto\n";
}
}
---------------
The result from this script is :
before sub toto=1
in sub toto=2
after sub toto=2
before sub toto=2
in sub toto=3
after sub toto=2
Why is it that the first time, the $toto++ inside the sub affects the
value outside the sub and not the second time?
Rgds,
Chacallot.
I though I understood the thing : when using a my $variable the scope is
the {} block.
I learned that the hard way while doing my first perl scripts and I
thought I understood it all thanks to perlintro "variable scoping"
(http://aspn.activestate.com/ASPN/docs/ActivePerl/5.8/lib/Pod/perlintro.html)
:
"The variables are scoped to the block (i.e. a bunch of statements
surrounded by curly-braces) in which they are defined."
So can someone give me a hint or a link to the rite (f) manual about how
to understand the result of the following example.
----------------
use strict;
for (my $i=1;$i<3;$i++)
{
my $toto=$i;
print "before sub toto=$toto\n";
titi();
print "after sub toto=$toto\n";
sub titi
{
$toto++;
print "in sub toto=$toto\n";
}
}
---------------
The result from this script is :
before sub toto=1
in sub toto=2
after sub toto=2
before sub toto=2
in sub toto=3
after sub toto=2
Why is it that the first time, the $toto++ inside the sub affects the
value outside the sub and not the second time?
Rgds,
Chacallot.