Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: if (open(FH, $path)) { ?

by Ryszard (Priest)
on May 03, 2004 at 14:20 UTC ( #350018=note: print w/replies, xml ) Need Help??

in reply to if (open(FH, $path)) { ?

Have a look at perldoc -f -X, it has a whole bunch of tests that can be done on files: if (-r $filename) { #do something }

Replies are listed 'Best First'.
Re:x2 if (open(FH, $path)) { ? (I got the resource acquisition blues)
by grinder (Bishop) on May 03, 2004 at 14:50 UTC

    Note that this is a theoretical race problem. The code could potentially run the test and determine that the file is readable, writeable, whatever.

    In the intervening period between the test and the moment you attempt to open the file, the permissions might have changed, or the file itself may no longer be around.

    It's the classic resource acquisition problem. Sometimes, no amount of testing can tell you whether something will succeed. All you can do is try it, and see what happens. So, to a certain extent, the technique shown in the OP is the best way to go.

      Yup, agreed, i would s/theoretical// as there is an actual risk, and it can happen...

      To handle the negative case, one could include a die handler in the open statement to make sure everything is nice and graceful.

        If I add an if (open(FH, $path) or die "$!") { doesn't that kill the application?

        I still want it to print out the rest of the webpage.

        Update2: Nevermind.
Re: Re: if (open(FH, $path)) { ?
by bkiahg (Pilgrim) on May 03, 2004 at 14:25 UTC
    Thanks Ryszard this is a better way!
      Not really; many platforms allow additional settings to provide/forbid access not taken into account by -r, so the easiest way is actually to try the open. The problem with that is that some other error may crop up and you'll never find out about it because you are ignoring all errors; you may want to ignore only permissions errors and carp on any others:
      use Errno; if (open $fh, "<", "foo/bar/baz") { ... } elsif ($!{EPERM}) { print "no go!\n"; } else { print "error opening foo/bar/baz: $!"; }
      Of course, to be really uptight about complaining about unexpected errors but ignoring lack of read permission to the file, you should go on to make sure the EPERM didn't result from lack of access to one of the directories traversed, but I'm not going there...
Re: Re: if (open(FH, $path)) { ?
by jdporter (Chancellor) on May 03, 2004 at 15:17 UTC
    I don't see the point in testing whether a file is readable before attempting to open it for reading. It both adds unnecessary verbosity to the code and introduces the race condition as others have pointed out.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2023-01-30 18:54 GMT
Find Nodes?
    Voting Booth?

    No recent polls found