References and scope.

A

Adam

Hey all,

Forgive what may be a dumb question.

What actually happens when a variable goes out of scope? More
specifically, if I define a variable in a sub-routine (it is thus in
scope only within the subroutine, right?) and explicitly return a
reference to that variable, how can I dereference that return value
outside the subroutine and still have something meaningful?

Hope this makes sense and I can be enlightened!

Adam...
 
A

A. Sinan Unur

(e-mail address removed) (Adam) wrote in @posting.google.com:
if I define a variable in a sub-routine (it is thus in
scope only within the subroutine, right?) and explicitly return a
reference to that variable, how can I dereference that return value
outside the subroutine and still have something meaningful?

In a nutshell: Because Perl's garbage collector keeps a count of references
to the variable and only deallocates the variable if that count is zero.

Sinan
 
M

Matija Papec

X-Ftn-To: Adam

Hey all,

Forgive what may be a dumb question.

What actually happens when a variable goes out of scope? More
specifically, if I define a variable in a sub-routine (it is thus in
scope only within the subroutine, right?) and explicitly return a
reference to that variable, how can I dereference that return value
outside the subroutine and still have something meaningful?

You should dereference it as any other reference. :)

sub oneTwo {
my @arr = qw/one two/;
return \@arr;
}

my $aref = oneTwo();
print @$aref;
 
X

xhoster

Hey all,

Forgive what may be a dumb question.

What actually happens when a variable goes out of scope?

The variable's reference count is decremented. If that results in a zero
reference count, then the variable can be destroyed and the space reused.
More
specifically, if I define a variable in a sub-routine

I assume you mean that you declare the variable with "my" in a
subroutine. I wouldn't call that "define", because define seems like
it would be the opposite of undef'ing a variable, which has little to do
with scope.
(it is thus in
scope only within the subroutine, right?)

The *name* of the varible is only in scope within the subroutine.
The name of the variable is one thing that refers to the variable, but
not necessarily the only thing which refers to it.
and explicitly return a
reference to that variable, how can I dereference that return value
outside the subroutine and still have something meaningful?

Uh, by using a dereferencing operator or syntax. :)

I don't quite understand your question. Do you want to know that guts
behind how Perl manages this? Or the rationale for it? Or do you just
reassurance that indeed this is the case?

Xho
 
X

xhoster

Hey all,

Forgive what may be a dumb question.

What actually happens when a variable goes out of scope?

The variable's reference count is decremented. If that results in a zero
reference count, then the variable can be destroyed and the space reused.
More
specifically, if I define a variable in a sub-routine

I assume you mean that you declare the variable with "my" in a
subroutine. I wouldn't call that "define", because define seems like
it would be the opposite of undef'ing a variable, which has little to do
with scope.
(it is thus in
scope only within the subroutine, right?)

The *name* of the varible is only in scope within the subroutine.
The name of the variable is one thing that refers to the variable, but
not necessarily the only thing which refers to it.
and explicitly return a
reference to that variable, how can I dereference that return value
outside the subroutine and still have something meaningful?

Uh, by using a dereferencing operator or syntax. :)

I don't quite understand your question. Do you want to know the guts
behind how Perl manages this? Or the rationale for it? Or do you just
want reassurance that indeed this is the case?

Xho
 
T

Tad McClellan

Adam said:
What actually happens when a variable goes out of scope?


It isn't actually the variable that goes out of scope, it is
the *name* of the variable that goes out of scope.

More
specifically, if I define a variable in a sub-routine (it is thus in
scope only within the subroutine, right?)


Right, outside of the subroutine you can no longer use that
name to access the variable's value.

and explicitly return a
reference to that variable,


Now you don't *need* the name to get access, you now have a second
way to get to the variable's value, via the reference.

how can I dereference that return value
outside the subroutine and still have something meaningful?


Because Perl uses a "reference counting" type of garbage collection.

When in the subroutine there is one reference to the variable,
via its name.

When you return a reference, the ref count goes to 2, then the
name immediately goes out of scope because the subroutine is ending,
and the ref count goes to 1.

The variable's space is not reclaimed until the ref count reaches zero.
 

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,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top