http://qs1969.pair.com?node_id=350028


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

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.

  • Comment on Re:x2 if (open(FH, $path)) { ? (I got the resource acquisition blues)

Replies are listed 'Best First'.
Re: Re:x2 if (open(FH, $path)) { ? (I got the resource acquisition blues)
by Ryszard (Priest) on May 03, 2004 at 14:56 UTC
    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.
        It kills more than the if statement; if open is failing but your application isn't dying, you must be in an eval or something.