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

Ok, but what you have demonstrated below isn't a single execution of a
persistantly running script, it is multiple executions of a
non-persistent script....

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

See above...  you haven't shown us how your single execution loop works
within the persistent script, I suspect the issue can be resolved by
examining that.  For instance if you use a fork/exec model then you can
encapsulate the references that you need to go out of scope within the
fork, then when the fork exits your vars are cleared, but the global
ones persist.
 
> 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
> 

Right, or not make it global at all and then the lexical nature takes
over... still don't see how you are having your variables persist?

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

Again as long as the vars are scoped to the execution loop this will
occur automagically...

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

Still not convinced you need any such idiom...

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to