filehandles and defined 
Author Message
 filehandles and defined

In perl 4, I'd like to check if a filehandle has been "defined", in other words
is open. I would use this in a cleanup routine that can be called from any point
in my program.  It would be nice to be able to close all open filehandles, but if
a particular one is not open, you get a {*filter*} error message.

        if (defined(FH)) { close(FH); }

isn't legal syntax.

        if (defined(fileno(FH))) { close(FH); }

does not work if FH has never been opened.  This is another case where
filehandle != variable is confusing.

-- Dave

--



Mon, 17 Jun 1996 01:47:57 GMT  
 filehandles and defined

:
:In perl 4, I'd like to check if a filehandle has been "defined", in other words
:is open. I would use this in a cleanup routine that can be called from any point
:in my program.  It would be nice to be able to close all open filehandles, but if
:a particular one is not open, you get a {*filter*} error message.
:
:       if (defined(FH)) { close(FH); }
:
:isn't legal syntax.
:
:       if (defined(fileno(FH))) { close(FH); }
:
:does not work if FH has never been opened.  This is another case where
:filehandle != variable is confusing.

I notice that you said perl4.  What you have there works in perl5.

Meanwhile, if you do this:

        if (defined($fd = fileno(FH))) { close(FH); }

Then it will work.  It's still not goin to take care of filehandles you
don't know about.  You might experiment with this little program and its
functions.

#!/usr/bin/perl

$PTRLEN = length ( $_main{'_main'} );

<DATA>;

open(TMPR, "< .") || die;
open(TMPW, "> /dev/null") || die;


fileno STDERR;
fileno STDIN;
fileno STDOUT;
fileno DATA;
fileno TMPR;
fileno TMPW;
# fileno TMPD;

printf NOWHERE "fileno never is %d\n", defined($t = fileno(NEVEROPEN));
printf NOWHERE "fileno bogus is %d\n", defined($t = fileno(BOGUS));
printf NOWHERE "fileno tmpr  is %d\n", defined($t = fileno(TMPR));

close(NEVEROPEN);


%this_is_a_table = ();

&dumpfh('main');


$x
.

sub this_is_a_function {};

sub this_is_a_package'and_this_is_its_function {};

sub get_fhs {

    local($PrintMe) = 1;
    &dumpfh('main');

Quote:
}

foreach $fh ( &get_fhs() ) {
    $fd = fileno ($fh);
    printf "%3s %s\n", defined ($fd) ? $fd : "-", $fh;

Quote:
}

sub dumpfh {
    local($packname) = $_[0];
    local(*stab) = eval "*_${packname}";
    local($name, $ptr);
    local($bitsig);
    $Seen{$packname}++;
    while ( ($name, $ptr) =  each %stab ) {

        $bitsig = &stabits($ptr);
        printf "%10s\t%-20s\t%s\n", $bitsig, $packname, $name if $PrintMe;

        if ($bitsig =~ /^111000100[01]$/ ) {

        }

        if (
            $ptr         =~ /^StB\000/  &&
            length($ptr) == $PTRLEN     &&
            $name        =~ /^_/
           )
        {
            $name =~ s/^_//;
            &dumpfh($name,1) unless $Seen{$name};
        }
    }

Quote:
}

sub stabits {
    local($stabptr) = shift;
    local($retval ) = '';

    foreach $i ( 0..9 ) {
        $retval .= $pieces[$i] ? "1" : "0";
    }
    return $retval;

Quote:
}

__END__
blech
--

      "Will Hack Perl for Fine Food and Fun"
        Boulder Colorado  303-444-3212


Mon, 17 Jun 1996 05:04:45 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Determining if a filehandle is defined

2. Using variables to define FILEHANDLES

3. defined keys that aren't defined

4. PERLFUNC: defined - test whether a value, variable, or function is defined

5. PERLFUNC: defined - test whether a value, variable, or function is defined

6. Defining constant values in Perl using #DEFINE

7. How to test a hash of arrays for defined/not defined

8. defined (defined(%hash))

9. ref doesn't return FileHandle for FileHandles?

10. print to filehandle when filehandle is a $var

11. ANNOUNCE: FileHandle::Rollback - FileHandle with rollback and commit

12. Redirecting filehandle to multiple filehandles

 

 
Powered by phpBB® Forum Software