P0w3rK!d has asked for the wisdom of the Perl Monks concerning the following question:

I have a program which reads values from a database which are placed into a unique file in /tmp. The file name is echoed from the program when it is created. My perl script is attempting to grab the output from that command by redirecting STDOUT to a file that I can read later ( I need to read the file and process each line for FTP purposes - this code already works). When I execute this portion of code, it never prints out the "foo1". Am I losing STDOUT ..or is the code flaking out on me?
#!/usr/bin/perl # just make up a fake file here to create # and echo the filename of /tmp/foo $tabfile = "./tabfile"; $logfile = "/tmp/foo"; # send output to file @args2 = ("$tabfile"); open (STDOUT, ">$logfile") || die "could not open log\n"; unless (system(@args2) == 0) { print STDERR "system(@args2) failed: $? $!\n"; return; } close(STDOUT); print "foo1\n";

Replies are listed 'Best First'.
Re: Help: STDOUT flaking out?
by busunsl (Vicar) on May 17, 2001 at 17:56 UTC
    STDOUT is a predefined filehandle. When you open a new file with that handle, the old is closed.
    After that you close STDOUT explicitly, so nothing is printed.

    Don't use STDOUT to open a file, use a different name.

Re: Help: STDOUT flaking out?
by suaveant (Parson) on May 17, 2001 at 18:01 UTC
    or try this....
    #!/usr/bin/perl # just make up a fake file here to create # and echo the filename of /tmp/foo $tabfile = "./tabfile"; $logfile = "/tmp/foo"; # send output to file @args2 = ("$tabfile"); { local(*STDOUT); open (STDOUT, ">$logfile") || die "could not open log\n"; unless (system(@args2) == 0) { print STDERR "system(@args2) failed: $? $!\n"; return; } close(STDOUT); } print "foo1\n"; }
    That should temporarily store STDOUT away and when you leave the code block, return its original value... I believe... not tested

    Update you could also use select, open log filehandle OUT say, instead of STDOUT, then do select OUT; all output will go to OUT instead of STDOUT until you do select STDOUT; to put it back...
                    - Ant

      This still is not working. The file "tabfile" is a shell script that created the file "FTPData.20010517.111754". I am trying to capture that echoed output and redirect it into a file. Might there be a better way to do this with a pipe or some other mechanism? Please advise :)
      RESULT: $ ftp_flow.pl logfile = /tmp/ftp_flow.111753410117.log errfile = /tmp/ftp_flow.111753410117.err FTPData.20010517.111754 $ cat /tmp/ftp_flow.111753410117.log $ (the file is empty... it should contain "FTPData.20010517.111754" as the 1st line) CODE: # send output to file @args2 = ("$tabfile"); local(*LOGFILE); open (LOGFILE, ">$logfile") || die "could not open log\n"; select(LOGFILE); unless (system(@args2) == 0) { print STDERR "system(@args2) failed: $? $!\n"; return; } close(LOGFILE); print "foo1\n"; exit (0);
        you probably really want to just use backticks...
        # send output to file ###either open (LOGFILE, ">$logfile") || die "could not open log\n"; print LOGFILE `$tabfile`; if($?) { print STDERR "$tabfile failed: $?\n"; return; } close(LOGFILE); ##or `$tabfile > $logfile`; if($?) { print STDERR "$tabfile failed: $?\n"; return; } ## end or print "foo1\n"; exit (0);

                        - Ant

        You need to select(STDOUT); after your close(LOGFILE);. Right now, you're still printing to a closed filehandle...

        This works now: Thank you :)
        # send output to file @args2 = ("$tabfile"); open (LOGFILE, ">$logfile") || die "could not open log\n"; unless (system(@args2) == 0) { print STDERR "system(@args2) failed: $? $!\n"; return; } select(LOGFILE); close(LOGFILE);