Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, i have some doubt regarding the scope of variable. Please have a look at the perl code below
package pkg; sub y { @Array = []; $Temp = {}; # Ankle bracket $Temp->{Name} = "Fred"; $Temp->{Age} = 45; $Array[0] = $Temp; $Temp = {}; # Ankle bracket $Temp->{Name} = "Fred"; $Temp->{Age} = 45; $Array[1] = $Temp; return @List; } sub x { @List = y(); } 1; __END__
When'll the memory allocated for the variable Temp will be freed. If i'm calling subroutine x from some other package. Thanks, chand

Replies are listed 'Best First'.
Re: Scope of Hash variable
by merlyn (Sage) on Dec 21, 2000 at 19:53 UTC
    Just a few things wrong with your code. Fix these, and then see if you still have questions...
    • Lowercase package names are reserved for internal use only.
    • A subroutine named "y" will be confused with the built-in "y" operator.
    • @array should have been declared with "my"
    • Setting @array to a single element consisting of an empty anonymous array probably doesn't do what you think it does. You wanted @array = ()
    • Even that would be unneeded if you had simply used my @array; because that's the initial value anyway.
    • All the hoops with $temp can be eliminated by just using the anonymous hash constructor:
      $array[0] = { Name => 'Fred', Age => 45 };
    • You're returning @List which was never set. Did you mean to return @Array?
    • A subroutine named "x" makes it hard to get in to the built-in "x" operator
    • @List should be declared with my
    You need to fix all of these before you even start to worry about scope.

    -- Randal L. Schwartz, Perl hacker

      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.
        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..

Re: Scope of Hash variable
by Fastolfe (Vicar) on Dec 21, 2000 at 19:20 UTC
    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.
Re: Scope of Hash variable
by mrmick (Curate) on Dec 21, 2000 at 19:31 UTC
    It's seems a good idea (to me) to use lexical scoping in subroutines by using 'my'. This way, there's less (I'm always afraid to say 'no') chance of a global variable being inadvertantly clobbered.

    Another good practice is to use 'strict'. This will save a lot of debugging time and frustration.

    Mick
Re: Scope of Hash variable
by KM (Priest) on Dec 21, 2000 at 19:23 UTC
    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