clearing of all variables

J

justme

hi

i have a while loop inside a sub

sub 1 {
while(1)
{
# declaring of variables, arrays, hashes

last if somecondition
}


}#end sub

Firstly, when the loop encounters last command, will perl take care of
clearing
all the variables, arrays, hashes that was declared in the while loop?
will the subroutine clear that for me? or do i have to include "undef"
just before the sub returns? If I need to include undef , how do i
clear everything at once ??

thanks for any help
 
S

Sherm Pendley

justme said:
i have a while loop inside a sub

sub 1 {
while(1)
{
# declaring of variables, arrays, hashes

last if somecondition
}


}#end sub

Firstly, when the loop encounters last command, will perl take care
of clearing all the variables, arrays, hashes that was declared in
the while loop?

Assuming that those variables are local in scope - i.e., they were
declared with "my" - yes, they'll be cleaned up automagically.

See also:

perldoc -f my
"Private Variables via my()" in perldoc perlsub

sherm--
 
J

Jürgen Exner

justme said:
hi

i have a while loop inside a sub

sub 1 {
while(1)
{
# declaring of variables, arrays, hashes

last if somecondition
}


}#end sub

Firstly, when the loop encounters last command, will perl take care of
clearing
all the variables, arrays, hashes that was declared in the while loop?

That depends upon _how_ you declared the variables (as global or as local)
and what you mean by "clearing".

Assuming you declared the variables as local, then after the block has
closed those variables are not accessible any more (unless you are keeping
some references somewhere).
Because perl does its own garbage collection, the memory that was allocated
to those inaccessible variables will be reused for new variables. So yes,
perl cleans up after variables go out of scope.
However (unless something in the implementation has changed recently) the
interpreter will not return this memory to the OS. So no, perl does not
clean up.

jue
 
B

Ben Morrow

Quoth (e-mail address removed) (justme):
hi

i have a while loop inside a sub

sub 1 {

1 isn't a valid name for a sub. Please post real code.
while(1)
{

Preferred style is

while (1) {

(not that it *really* matters...).
# declaring of variables, arrays, hashes

last if somecondition

....;

Please post real code.
}


}#end sub

Firstly, when the loop encounters last command, will perl take care of
clearing
all the variables, arrays, hashes that was declared in the while loop?

Lexical variables ('my' variables) are destroyed at the exit of their
innermost enclosing lexical scope (set of braces or file). So my
variables declared inside the loop will be destroyed when the loop is
exitted, however that occurs (falling off the end, 'last', 'die', etc.).

Globals (package variables, undeclared variables if you're not using
'strict' (you should be), 'our' variables) are never destroyed.

[Dynamicly-scoped values (what you get when you apply 'local' to a
variable) have their old value restored at the exit of their innermost
enclosing lexical scope, but you don't even want to *think* about them
until you understand scope rather better than you do now.]
will the subroutine clear that for me?

Variables declared at sub scope will be destroyed when the sub exits.

Ben
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top