Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Unexpected behavior with open

by BlueLines (Hermit)
on Apr 14, 2001 at 05:58 UTC ( [id://72536]=perlquestion: print w/replies, xml ) Need Help??

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

So i was tracking down a bug in a module i wrote (which ended up being a non-closed file handle) and i noticed this. Take the following code:
#!/usr/bin/perl -wT use strict; sub foo { open(FOO, '>/tmp/foo') or die "$!\n"; print FOO "this is a test\n"; } sub bar { open (BAR, '/tmp/foo') or die "$!\n"; while (<BAR>) { print; } } foo(); bar();
This code produces no output untill i add a close FOO; in &foo. However, the following code does work:
#!/usr/bin/perl -Tw use strict; sub foo { open(FOO, '/tmp/foo') or die "$!\n"; while (<FOO>) { print; } } sub bar { open (BAR, '/tmp/foo') or die "$!\n"; while (<BAR>) { print; } } foo(); bar();
So what's going on here? Why can't I open the same file with two different modes when I can open the same file in the same mode? And why doesn't the second open barf? I tried this in linux under both 5.00053 and 5.6 and got the same results.

BlueLines

Disclaimer: This post may contain inaccurate information, be habit forming, cause atomic warfare between peaceful countries, speed up male pattern baldness, interfere with your cable reception, exile you from certain third world countries, ruin your marriage, and generally spoil your day. No batteries included, no strings attached, your mileage may vary.

Replies are listed 'Best First'.
Re: Unexpected behavior with open
by merlyn (Sage) on Apr 14, 2001 at 06:02 UTC
      or you can use the idiom
      select((select(FOO),$|=1)[0]);
      does "idiom" mean "obfuscation"?
Re: Unexpected behavior with open
by Dominus (Parson) on Apr 14, 2001 at 09:28 UTC
Re: Unexpected behavior with open
by BlueLines (Hermit) on Apr 14, 2001 at 07:39 UTC
    Argh. Someone at my work actually pointed out the buffering issue. We actually found that executing a shell process flushed the buffer as well. Sorry for the dumb post...

    BlueLines

    Disclaimer: This post may contain inaccurate information, be habit forming, cause atomic warfare between peaceful countries, speed up male pattern baldness, interfere with your cable reception, exile you from certain third world countries, ruin your marriage, and generally spoil your day. No batteries included, no strings attached, your mileage may vary.
Re: Unexpected behavior with open
by kha0z (Scribe) on Apr 14, 2001 at 08:06 UTC
    Just as it is good practice to declare your variables and initiallize them. If is good practice to close your file handles.

    This is just my 2 cents.

    Good Hunting,
    kha0z

      FWIW I only rarely close my filehandles. Instead I scope them and let Perl take care of the cleanup.

      This does depend on reliable destruction semantics though, and in Perl 6 I may be forced to do that work for Perl. :-(

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (1)
As of 2024-04-24 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found