david wrote:

Jupiterhost.Net wrote:


Ok, well you think that might have helped to state in the first place,
especially when posting to a beginners list?

Sorry I didn't mean to offend anyone, I felt it was irrelevant to the question. (IE - How do I vacuum my car instead of How would I vacuum a blue car?)



no need to apologize, you didn't offend anyone. i read the thread and i agree with you that your question of how to undef a ref is irrelevant to how you manage a persistent process via PersistentPerl or FCGI or mPerl.


I simply was trying to figure out the best way to undef/close/other wise
destroy each ref in a list of refs depending on the type of reference. I
figured the way I was doing it didn't matter because either way all I
want to do is:

undef ${$_} if ref($_) eq 'SCALAR';
I can do the above for SCALAR, ARRAY, HASH
but the question is what do I do if it's one of these:
 IO, GLOB, or CODE

So this would work for a SCALAR, ARRAY, HASH, GLOB, CODE, IO, and REF?



for(@refs_to_destroy) { close $_[0] if ref($_[0]) eq 'IO'; undef $_[0] if ref($_[0]); }

#!/usr/bin/perl

use strict;
use warnings;

my $howdy = 'hello';
push(@ref_to_destroy, \$howdy);
for(@refs_to_destroy) {
  my $r = ref($_);
    if(defined $r) {
      close $_ if $r eq 'IO';
      undef $_ if $r eq 'SCALAR';
    }
}
print "I still have howdy -$howdy-\n" if defined $howdy;

If I do undef ${ $_ } if ... then it works (IE you get no "I still have" message..
Which means I have to do @{ $_ } %{ $_ } etc also... The etc part is the one throwing me now :)





no need to do that, you don't want to undef what $_ points to, you want to undef $_ itself. for example:

#!/usr/bin/perl -w
use strict;

$_ = {a => b => c => 1};

undef %{$_};

print "still a ref\n" if ref $_;

__END__

prints:

still a ref

simply:

* undef $_ is fine
* in fact, you can loose a reference count (Perl uses reference counting to gc) by simply pointing $_ to somewhere like:


$_ = 1;

in turns of ref to IO, you can do similiar like:

#!/usr/bin/perl -w
use strict;

sub decrease_ref_count{
        my $r = ref $_[0];
        return unless $r;
        close $_[0] if $r =~ /IO/;
        undef $_[0]; #-- or $_[0] = 0;
}

$_ = *STDIN{IO};

decrease_ref_count($_);

1 while(<STDIN>); #-- STDIN already closed

__END__


1) Are there any other ref() type's I am missing?



yes. ref to ref or ref to objcts etc, consider:

$_ = \\1;

print ref $_,"\n"; #-- prints REF

but the inner ref is lost when you loose the outer ref.


2) If the reference is to an open file handle (\*FH):
 a) it will ref() as a GLOB correct (or IO)?


it will be a GLOB is it's GLOB and it will be an IO if it's an IO, depends on how you set up your ref:

print ref \*STDIN,"\n";    #-- GLOB
print ref *STDIN{IO},"\n"; #-- IO::Handle

be sure to write code that handles both


 b) Will it be closed if I handle it like any other GLOB or do I need
to somehow check if its an open filehandle and close it?


i would check for IO and close it if needed



Excellent info David I very much appreciate it..
Now to play around with my new found knowlege!

HAGO

Lee.M - JupiterHost.Net
david

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