Perl variable scope

H

howa

Hi,

Conside the following simple program:

#-----------------------------------------------------
sub test {
my $i = "999";
return \$i;
}

print ${test()};
#-----------------------------------------------------

i can still get "999", althought $i is a local variable in a procedure.

i want to ask : is it safe to use reference in the way?

or when the garbage collection will clear the reference?

thanks...
 
A

anno4000

howa said:
Hi,

Conside the following simple program:

#-----------------------------------------------------
sub test {
my $i = "999";
return \$i;
}

print ${test()};
#-----------------------------------------------------

i can still get "999", althought $i is a local variable in a procedure.

i want to ask : is it safe to use reference in the way?

or when the garbage collection will clear the reference?

This is a FAQ. See "perldoc -q safe".

Anno
 
H

howa

howa 寫é“:
(e-mail address removed)-berlin.de 寫é“:


great thanks...


one more thing...

if a variable type is an object, will be pass by reference by default?

e.g.


return $a; # where $a is an object, will this pass by ref by default?


(php and java will do this automatically)

thx..
 
A

anno4000

howa said:
howa 寫é“:
(e-mail address removed)-berlin.de 寫é“:
[...]

one more thing...

if a variable type is an object, will be pass by reference by default?

e.g.

"Pass by reference" is a term that has to do with the passing of
parameters into a subroutine. Perl always passes parameters by
reference. This has nothing to do with whether the parameter itself
is a reference or a plain scalar.
return $a; # where $a is an object, will this pass by ref by default?

Returning values is something else again.
(php and java will do this automatically)

Perl doesn't treat objects any differently from other values.

Anno
 
H

howa

(e-mail address removed)-berlin.de 寫é“:
howa said:
howa 寫é“:
(e-mail address removed)-berlin.de 寫é“:

howa <howachen@gmail.com> wrote in comp.lang.perl.misc:
[...]

one more thing...

if a variable type is an object, will be pass by reference by default?

e.g.

"Pass by reference" is a term that has to do with the passing of
parameters into a subroutine.
Perl always passes parameters by reference.

are you sure about this point?

i.e.

sub test {
my $i = shift;
print \$i;
}


my $j = 999;
print \$j;

test($j);
 
C

Ch Lamprecht

howa said:
(e-mail address removed)-berlin.de 寫é“:

howa said:
howa 寫é“:


(e-mail address removed)-berlin.de 寫é“:


[...]


one more thing...

if a variable type is an object, will be pass by reference by default?

e.g.

"Pass by reference" is a term that has to do with the passing of
parameters into a subroutine.
Perl always passes parameters by reference.


are you sure about this point?

i.e.

sub test {
my $i = shift;
print \$i;
}
sub test{
print \$_[0];
}

my $j = 999;
print \$j;

test($j);

Christoph
 
A

anno4000

howa said:
(e-mail address removed)-berlin.de 寫é“:
howa said:
howa 寫é“:

(e-mail address removed)-berlin.de 寫é“:

[...]

one more thing...

if a variable type is an object, will be pass by reference by default?

e.g.

"Pass by reference" is a term that has to do with the passing of
parameters into a subroutine.
Perl always passes parameters by reference.

are you sure about this point?

Yes, I am.
i.e.

sub test {
my $i = shift;
print \$i;
}


my $j = 999;
print \$j;

test($j);

The references are different. That's because the lexical variable $i
in the sub is different from $j outside. You need to access the first
argument directly to see what it is. Change test() like this:

sub test {
print \ $_[ 0];
my $i = shift;
print \$i;
}

and run it again. The first reference printed by test() is identical
to the one you feed it.

Anno
 
H

howa

(e-mail address removed)-berlin.de 寫é“:
howa said:
(e-mail address removed)-berlin.de 寫é“:
howa 寫é“:

(e-mail address removed)-berlin.de 寫é“:


[...]

one more thing...

if a variable type is an object, will be pass by reference by default?

e.g.

"Pass by reference" is a term that has to do with the passing of
parameters into a subroutine.
Perl always passes parameters by reference.

are you sure about this point?

Yes, I am.
i.e.

sub test {
my $i = shift;
print \$i;
}


my $j = 999;
print \$j;

test($j);

The references are different. That's because the lexical variable $i
in the sub is different from $j outside. You need to access the first
argument directly to see what it is. Change test() like this:

sub test {
print \ $_[ 0];
my $i = shift;
print \$i;
}

and run it again. The first reference printed by test() is identical
to the one you feed it.

Anno

thanks!!

last thing, is it possible to return by reference value?

e.g.

sub test {
my $i = "123";

return \$i;
}

my $j = test();

print $j ;# i don't want the reference, i want "123", and i don't want
to use ${$j}


thanks so much...
 
A

anno4000

howa said:
(e-mail address removed)-berlin.de 寫é“:

last thing, is it possible to return by reference value?

e.g.

sub test {
my $i = "123";

return \$i;
}

my $j = test();

print $j ;# i don't want the reference, i want "123", and i don't want
to use ${$j}

You'll have to use it (or $$j). Perl doesn't de-reference things for
you automatically (the one exception is globrefs used as filehandles).

You should also keep apart the concepts of a Perl "reference" and the
concept of "passing by reference". While both use the term "reference",
they describe different things. What is normally called "passing by
reference" is called "aliasing" in Perl terminology and doesn't involve
referencing and de-referencing in the Perl sense.

Anno
 
H

howa

(e-mail address removed)-berlin.de 寫é“:
You'll have to use it (or $$j). Perl doesn't de-reference things for
you automatically (the one exception is globrefs used as filehandles).

You should also keep apart the concepts of a Perl "reference" and the
concept of "passing by reference". While both use the term "reference",
they describe different things. What is normally called "passing by
reference" is called "aliasing" in Perl terminology and doesn't involve
referencing and de-referencing in the Perl sense.

Anno

Thanks...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,260
Messages
2,571,305
Members
47,953
Latest member
TrinidadV1

Latest Threads

Top