Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

close STDOUT and system equal strange?

by jptxs (Curate)
on Feb 21, 2004 at 23:12 UTC ( #330861=perlquestion: print w/ replies, xml ) Need Help??
jptxs has asked for the wisdom of the Perl Monks concerning the following question:

this:

close STDOUT; close STDERR; open ( FILE, ">>./testing.stdout" ) or die "$!\n\n"; print FILE "this\n"; system('ls') == 0 or print FILE "oops $!\n"; print "that\n";

results in:

this DBVARS.main getServername.pl logs nssswitch.pl nssswitch.sh testing.stdout tryCloseSDTOUT.pl

I get why "that\n" doesn't print, but why does the system output appear in my file? Apparently, it's because it has nowhere else to go. My usual methods of investigation have turned up dry...

We speak the way we breathe. --Fugazi

Comment on close STDOUT and system equal strange?
Select or Download Code
•Re: close STDOUT and system equal strange?
by merlyn (Sage) on Feb 21, 2004 at 23:22 UTC
    You're trying to pull a fast one on the kernel, and you lost. "standard output" is file descriptor 1. When you open a new file, it goes to the lowest unopened file descriptor. Normally, 0, 1, and 2 are all busy being standard input/output/error, but by closing STDOUT, you left room for another file to be fd 1. Hence, you lose. If you don't want that behavior, don't do that.

    If you want to ensure that the child's standard output goes nowhere, open STDOUT onto /dev/null instead.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      For the record, I wasn't trying =]

      I was happy to see it in the file in the larger program where I saw this happen b/c I had been planning on redirecting it there anyway. I suppose I could leave it this way, but I probably will not. It seems like a "bad way to do it".

      thanks for the enlightenment, merlyn.

      We speak the way we breathe. --Fugazi
      or File::Spec->devnull();

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://330861]
Approved by valdez
Front-paged by blue_cowdawg
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-09-20 14:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (159 votes), past polls