in reply to Help: STDOUT flaking out?

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

Replies are listed 'Best First'.
Re: Re: Help: STDOUT flaking out?
by P0w3rK!d (Pilgrim) on May 17, 2001 at 19:23 UTC
    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
        Thank you. I removed the code which was screwing up STDOUT. It all works now. :)

      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);
        In reference to the previous postings: After I cat the log file I create, it shows that not only the 1st line which was grabbed from the $tabfile output, but also the output from:
        print "foo1\n";
        and
        print "GOT THIS: $lines\n";
        RESULT: $ ftp_flow.pl logfile = /tmp/ftp_flow.12547410117.log errfile = /tmp/ftp_flow.12547410117.err $ cat /tmp/ftp_flow.12547410117.log FTPData.20010517.120547 foo1 GOT THIS: FTPData.20010517.120547 $
        How do I set STDOUT back to normal?
        # 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; } select(STDOUT); close(STDOUT); print "foo1\n"; # read the file to obtain the file tabdata created local(*LOGFILE); open (LOGFILE, $logfile) || die "Couldn't open: $!"; $lines=<LOGFILE>; #get line 1 from the file close(LOGFILE); print "GOT THIS: $lines\n"; exit(0);