Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: is filehandler used ?

by Tanktalus (Canon)
on Mar 01, 2006 at 17:42 UTC ( #533708=note: print w/replies, xml ) Need Help??


in reply to is filehandler used ?

First, my confusion: "filehandler" is something that handles files, and "filehandle" is a handle to a (or token for a) file. You have a filehandle.

Second, the solution: Using fileno works fine for files. On Linux/unix, it probably works for sockets, fifos, and many other things. On no platform does it work for IO::String or handles to a string buffer (e.g., open OUT, '>', \$buffer). If you are only worried about real files, that's fine.

Instead, you will pretty much need to keep the state seperate. For example, using a symref:

my $fh; open $fh, "<", "/etc/shadow" or $fh = undef; if ($fh) { # do stuff }
Now it doesn't matter what you have that you're reading from - failure results in an undef. (Not quite true - if you start using the forking version of open, such as "-|" or "|-", then things have to work a bit differently.)

Personally, I just merge it all:

if (open my $fh, "<", "/etc/shadow") { # do stuff. close $fh; }
Anywhere inside the if, the filehandle is good. Anywhere outside, the filehandle doesn't even exist.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://533708]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2019-03-26 02:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How do you Carpe diem?





    Results (122 votes). Check out past polls.

    Notices?