Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

unable to open existing file after trying to open a non-existing one

by monk_of_gaia (Initiate)
on Sep 23, 2003 at 15:26 UTC ( #293530=perlquestion: print w/replies, xml ) Need Help??

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

1. I open an existing file. Result: OK
2. I try to open or test the existance of a non-existing file with "-e"
3. I open an existing file. Result: ERROR
I tried it with Perl v5.003_07 for Win32 Build 316, Perl v5.004_02 Win32 and 5.005_03 built for MSWin32-x86-object build 518. I have BMC Patrol on this Win2k machine, and these Perls comes with BMC Patrol. It is not possible at this moment to upgrade Perl on this machine.
The test program:
open TT1, "<test.out"; if ($! == 0) { print "1. [OK]\n"; @x = <TT1>; close TT1; } else { print "[ERROR]\n"; } if (-e "tt1") { open TT2, "<tt1"; if ($! == 0) { print "2. [OK]\n"; close TT2; } else { print "2. [ERROR]\n"; } } open TT1, "<test.out"; if ($! == 0) { print "3. [OK]\n"; @x = <TT1>; close TT1; } else { print "3. [ERROR]\n"; }
test.out is an existing file, tt1 is not. The output is:
1. [OK] 3. [ERROR]
If I comment out the middle part, the if (-e "tt1") { ... and just simply open & read & close the file twice, I get:
1. [OK] 2. [OK]
This is more, than annoying and more than weird. Could you please suggest any workarounds?

Replies are listed 'Best First'.
Re: unable to open existing file after trying to open a non-existing one
by liz (Monsignor) on Sep 23, 2003 at 15:40 UTC
    You're checking $! without checking whether the open() failed! From the documentation:

    This means that the value of $! is meaningful only immediately after a failure:

    if (open(FH, $filename)) { # Here $! is meaningless.

    Liz

      ok, thanks :)
Re: unable to open existing file after trying to open a non-existing one
by pzbagel (Chaplain) on Sep 23, 2003 at 15:48 UTC

    Welcome from the shell scripting world. I know that $! has been your trusty friend for a long time, but Perl relies on return values. The $! serves to communicate error information about failures but it itself does not return true or false regarding successes or failures of specific system calls. If you want to check for success or failure, you should really capture the return value of the open. Although, in Perl, we rarely do that, instead you'd feed it right into an if-then-else statement:

    open TT1, "<test.out" ? do { print "1. [OK]\n"; @x = <TT1>; close TT1; } : print "[ERROR]\n";

    Notice how we get the desired behavior. You can do the same for the other sections as well.

    HTH

Re: unable to open existing file after trying to open a non-existing one
by asarih (Hermit) on Sep 23, 2003 at 15:42 UTC
    For one thing, I think you're confusing Perl's open and C's open. Perl's open returns 1 if successful, whereas C's open() returns 0.

    Update: While the above statement is true, the OP is not confused. I'm the one who is confused about what OP's code was doing. Apologies.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2022-09-27 16:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (122 votes). Check out past polls.

    Notices?