
nevildev
Novice
Aug 20, 2010, 4:41 AM
Post #1 of 4
(483 views)
|
|
Are local variables automatically deleted after a subroutine ends?
|
Can't Post
|
|
Hello, Sorry if this has been posted before. This is a concept I've never really fully grasped. In many of my scripts, massive local hash values are created within subroutines. At the end of every subroutine, I've always explicitly deleted the local hash values in fear of unnecessarily using too much memory. Below is sample code to demonstrate what I mean. Is it necessary to remove local hash variables, or are they automatically deleted at the end of a subroutine?
#!/usr/bin/perl sub test { #Define local variables my (%file_local,%hash_local); my $i; $file_local{'hash_output'} = "hash_dump.txt"; open ( OUT_FILE, ">$file_local{'hash_output'}" ) or die "Can't open $file_local{'hash_output'}"; print OUT_FILE "i,i+1\n"; for ($i = 1; $i <= 100000; $i++) { $hash_local{$i} = $i-1; print "$i,$hash_local{$i}\n"; } close ( OUT_FILE ); #Remove local variables %file_local = %hash_local = (); } print "Exiting program gracefully ...\n"; exit 1; Thanks! Evan
|