Wiggins d Anconia wrote:
Hello,
I have an array that contains one reference per item. I need to clean up the things referenced by those references So.. 1) Am I doing this correctly? 2) am I doing the CODE,IO correct 3) any other refs I'm overlooking?
for(@references_to_kill) {
undef ${$_} if ref($_) eq 'SCALAR';
undef @{$_} if ref($_) eq 'ARRAY';
undef %{$_} if ref($_) eq 'HASH';
undef *{$_} if ref($_) eq 'GLOB';
undef &{$_} if ref($_) eq 'CODE'; # is this right??
close $_ if ref($_) eq 'IO'; # is this right, what if it was never opened? How can I check that?
# any others,
}
My question is why do you need to do this? Generally it may indicate either a problem with scoping or a design flaw.
I knew someone would ask me that :)
Its because at the end of a single execution of a persistantly running script I can leave the globals in tact for the next time or kill them.
For instance, I create $dbh and leave it alone and it's available when someone else calls it, ame thign with File handles.
For instance I could do: use vars '$test'; if(!defined $test) { print "Initializing var...\n"; $test = randstr(); } print "$test\n";
then I do:
./test.pl Initializing var... abcdefghijk ./test.pl abcdefghijk ./test.pl abcdefghijk
which is great for say a persistent database connection but if I want a var to be set each time I could just do:
use vars '$test'; $test = randstr(); print "$test";
./test.pl abcdefghijk ./test.pl asdhjbgasdc ./test.pl weiuervbnef
In this simple example cleaning up the way I'm trying to do is a bit over kill but in a more complex arena I'd like to simply list the references to things I want 100% cleaned up after each time it is executed (even if it is reset at the beginning of the run).
From perldoc perlref:
"Hard references are smart--they keep track of reference counts for you, automatically freeing the thing referred to when its reference count goes to zero. (Reference counts for values in self-referential or cyclic data structures may not go to zero without a little help; see "Two-Phased Garbage Collection" in perlobj for a detailed explanation.)
I'll have to check that out, so i may be able to simply do:
for(@refs_to_kil) { #get the "refernce count to zero" here in one line regardless of ref type }
If that thing happens to be an object, the object is destructed. See perlobj for more about objects."
http://danconia.org
Thanks for the info :)
Lee.M - JupiterHost.Net
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>