Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^4: STDERR Restore after redirect

by tultalk (Monk)
on Apr 28, 2018 at 02:19 UTC ( [id://1213713]=note: print w/replies, xml ) Need Help??


in reply to Re^3: STDERR Restore after redirect
in thread STDERR Restore after redirect

Thanks

I tried the local before and it did not work. Will try again

I put some code in the sub that fails to print:

sub CreateUserFeedbackForm { my ($message) = @_; if(!defined('STDOUT')){ warn("No STDOUT"); } elsif (defined('STDOUT')){ warn("STDOUT Defined"); } if(!defined('STDERR')) { warn("No STDOUT"); } elsif (defined('STDERR')) { warn("STDERR Defined"); }

Result in my error log below:

Fri Apr 27 16:43:31 2018 manage_users.cgi: STDOUT Defined at manage_users.cgi line 806. STDERR Defined at manage_users.cgi line 814. Fri Apr 27 16:43:31 2018 manage_users.cgi: STDERR Defined at manage_users.cgi line 814. At exit of sub CreateUserFeedbackForm at manage_users.cgi line 964. Fri Apr 27 16:43:31 2018 manage_users.cgi: At exit of sub CreateUserFeedbackForm at manage_users.cgi line 964.

Since both returned "defined" my next question: Does that mean they are functional?

I guess I could try writing to the handles to see but thought maybe some Monk could say "Yes defined means it is functional"

Thanks

Replies are listed 'Best First'.
Re^5: STDERR Restore after redirect
by AnomalousMonk (Archbishop) on Apr 28, 2018 at 03:23 UTC
    if(!defined('STDOUT')){ ... }

    The strings  'STDOUT' and  'STDERR' are literal strings and not file handles. Strings are always defined. Even '' (the empty string) and '0', both of which are false, are both defined.

    I'm not sure of the best way to test if a file handle is functional, nor am I quite sure what "functional" means in this context.


    Give a man a fish:  <%-{-{-{-<

      Is this not a test for a file handle?

      open my $oldSTDOUT, ">&STDOUT"; open OLDERR, ">&",\*STDERR; open(STDOUT, ">&", $oldSTDOUT) or warn("Can't open STDOUT"); + open(STDERR, ">&OLDERR") or warn("Can't open STDERR");

      Neither published a warn

        Is this not a test for a file handle?

        I would say no. Neither of these statements indicate failure in any way:
            open my $oldSTDOUT, ">&STDOUT";
            open OLDERR, ">&",\*STDERR;
        AFAIU, both of these statements indicate failure of file handle restoration if it occurs, but give no indication that the handle was valid prior to restoration:
            open(STDOUT, ">&", $oldSTDOUT) or warn("Can't open STDOUT");
            open(STDERR, ">&OLDERR") or warn("Can't open STDERR");

        I still don't really understand the basic problem. Maybe you can find something in the following:

        c:\@Work\Perl\monks>perl -wMstrict -le "open oldSTDOUT, '>&', \*STDOUT or warn qq{duping STDOUT: $!}; print oldSTDOUT 'hi there everybody'; print STDOUT '... and welcome'; print '... to the show.'; if (defined *oldSTDOUT) { warn 'A: *oldSTDOUT is defined'; } ;; close STDOUT or warn qq{closing STDOUT: $!}; print STDOUT 'how now'; print '... brown cow.'; if (defined *STDOUT) { warn 'B: *STDOUT is defined'; } ;; open STDOUT, '>&', \*oldSTDOUT or warn qq{re-opening STDOUT: $!}; print oldSTDOUT 'aloha'; print STDOUT '... sweet dreams'; print '... and goodbye.'; if (defined *oldSTDOUT) { warn 'C: *oldSTDOUT is defined'; } if (defined *STDOUT) { warn 'D: *STDOUT is defined'; } " hi there everybody ... and welcome ... to the show. A: *oldSTDOUT is defined at -e line 1. print() on closed filehandle STDOUT at -e line 1. print() on closed filehandle STDOUT at -e line 1. B: *STDOUT is defined at -e line 1. aloha ... sweet dreams ... and goodbye. C: *oldSTDOUT is defined at -e line 1. D: *STDOUT is defined at -e line 1.


        Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-25 15:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found