Just a few things wrong with your code. Fix these, and then see if you still
have questions...
You need to fix all of these before you even start to worry about scope.
-- Randal L. Schwartz, Perl hacker | [reply] [d/l] |
Thanks for your reply
Sorry, i made the mistakes you have mentioned. I just want to convey the logic only.
@Array and $Temp should be declared "my". And i would like to return @Array only not @List. And i'm using $Temp stuff for useability purpose only.
I've some where studied that the ankle bracket constructor is equivalent to the malloc() in C.
Can you please tell me when'll be the memory, allocated for $Temp, freed.
| [reply] |
As a Perl developer, you shouldn't have to worry about such things. If you're building something that will be using up some large amounts of memory, and you want to be sure that memory is only used while you're in a certain chunk of code, scope that variable within that chunk of code:
sub my_sub {
my $variable;
...
}
# or
if ($some_condition) {
my $other_variable;
...
}
# $other_variable disappears when 'if' block exits!
In this example, $variable will disappear when you leave that subroutine. The memory will not be free'd in that your process's memory usage will decrease, but the memory it was using is now available to be claimed by other variables in your code.
Generally Perl's garbage collection is meant to keep the tasks of memory management away from you, so you don't have to worry about it. So long as you use strict, Perl will just about force you to declare your variables, which means you have the opportunity to scope them where they are appropriate, and let them disappear when you're done with them. You may wish to get your hands on some Perl books that talks about this in more detail, and I think it's been covered on the site a few times, so a Super Search may give you some more help.
Hope this answers your question.. | [reply] [d/l] [select] |
Well since you're not locally scoping any of your variables, I imagine they will all be global forever. Perhaps you are messing yourself up by returning @List from your subroutine instead of @Array? PLEASE 'use strict'. It would have caught this mistake. | [reply] [d/l] [select] |
| [reply] |
The variable is global, and should stick around until Perl sees nothing
referencing that variable. When Perl sees a variable is not going to be used anymore, and
that nothing is referencing it in memory, it should take care of it.
Cheers,
KM | [reply] |