shemp has asked for the wisdom of the Perl Monks concerning the following question:
Basically i am trying to figure out when a function is called, and then exited, what sticks around for subsequent calls, and what is destroyed and needs to be recreated internally on each call.
I'm assuming that @list is not going to ever change in blah().# case 1 sub blah { ... my @list = qw(# list of things); foreach my $item (@list) { ... } # case 2 { my @list; BEGIN { @list = qw(# list of things); } sub blah { ... foreach my $item (@list) { ... } } # case 3 sub blah { ... foreach my $item qw(# list of things) { ... }
In case 1, im pretty sure that @list is re-initialized on each call to blah()
In case 2, @list is initialized once, in the BEGIN{}, and never goes away, because of the closure.
case 3, i am not sure what happens. Does the inline list re-create itself on each call to blah(), or is it created once and sticks around somewhere? I'm uncertain about this one because the list is never assigned to a variable (as a whole), which makes me think that its sticking around somewhere.
I could ask similar questions about inlined regexs, etc.
UPDATE:
changed my blah {} to sub blah {} - as it should be.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Resource allocation question
by revdiablo (Prior) on Oct 20, 2004 at 18:09 UTC | |
|
Re: Resource allocation question
by ikegami (Patriarch) on Oct 20, 2004 at 19:51 UTC | |
by shemp (Deacon) on Oct 20, 2004 at 20:06 UTC | |
|
Re: Resource allocation question
by JediWizard (Deacon) on Oct 20, 2004 at 17:53 UTC | |
by shemp (Deacon) on Oct 20, 2004 at 17:58 UTC |