Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

valid filehandle

by Anonymous Monk
on Mar 20, 2003 at 23:38 UTC ( #244763=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I'm trying to check whether a filehandle from open is valid after the call. If the filehandle is valid, I want to use it to print with, if it is not I want to do something else. For some reason, the below code segment doesn't work. It always goes to the else part of the code.
open($test, "<test.txt"); if (!$test) { #do something } else { #use $test to print something }
I know i can do this:
open($test, "<test.txt") || die("$!\n");
but i really need to check for a valid filehandle after the open is complete. Is there a way to do this? thanks, Michael

Replies are listed 'Best First'.
Re: valid filehandle
by jsprat (Curate) on Mar 20, 2003 at 23:47 UTC
    Update: You do realize 1: this code opens the file for read, not write and 2: If the file open is successful the else clause will be executed?

    Sure - the filetests (-f in this case) work on filehandles as well as filenames.

    if (-f $test) { print $test "It's a file"; }

    See perldoc perlfunc(search for -X) for more info on all the filetest operators.

Re: valid filehandle
by DaveH (Monk) on Mar 20, 2003 at 23:49 UTC

    Hi.

    I'd use some variation of IO::File or IO::Handle. e.g.

    use IO::File; my $fh = IO::File->new("<test.txt"); if (!$fh) { #do something print "my file didn't open\n"; } else { #use $test to print something print "my file did open\n"; while (<$fh>) { # ... } }

    I hope that helps.

    Cheers,

    -- Dave :-)


    $q=[split+qr,,,q,~swmi,.$,],+s.$.Em~w^,,.,s,.,$&&$$q[pos],eg,print
Re: valid filehandle
by Limbic~Region (Chancellor) on Mar 20, 2003 at 23:51 UTC
    Michael (a.k.a. Anonymous Monk),
    It depends on what you mean by valid filehandle. If you want to see if the open worked, you could simply do this:
    #!/usr/bin/perl -w use strict; my $Open_Ok; if (open (FILE,"file.txt")) { $Open_Ok = "ok"; } else { $Open_Ok = "not ok"; } print "$Open_Ok\n";

    Of course if you don't add any more to the program, you are going to get a warning about using FILE only once, but that is ok.

    If on the other hand, you want to know if there is any data left in the file to read, you want to check out eof.

    If you want to check to see if the file exists and is readable, then you want to check out perldoc -f -X - pay attention to -e and -r.

    If you are interested in finding out if a read on the filehandle will result in a block, you should check out the 4 arg select

    I hope this helps and cheers - L~R

Re: valid filehandle
by BrowserUk (Patriarch) on Mar 21, 2003 at 00:48 UTC

    If you have a filehandle (to a real file rather than a terminal or pipe etc) that was opened elsewhere and you want to simply test if it is still valid before attempting to use it, you can use tell. The function will return -1 if the file has been closed, was never opened, and produces no warning messages.


    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
      fileno() would be safer since tell() "on pipes, fifos, and sockets usually returns -1";
      although in his particular case either one would work.

      --perlplexer
Re: valid filehandle
by Vorlin (Beadle) on Mar 21, 2003 at 00:46 UTC
    You can also do this:

    if (!open FILE, "<test.txt") { # do something if can't open } else { # do something if it is opened for reading }
    As you can see, other solutions are available as well.
Re: valid filehandle
by Anonymous Monk on Mar 21, 2003 at 14:41 UTC
    Thanks so much for all your replies. the comments are all very helpful (some more than others). you guys rule as always. michael

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://244763]
Approved by dga
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2023-06-02 18:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?