Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: STDERR Restore after redirect

by choroba (Cardinal)
on Apr 26, 2018 at 22:12 UTC ( [id://1213646]=note: print w/replies, xml ) Need Help??


in reply to STDERR Restore after redirect

Store stdout and stderr to variables, restore them later. No select is needed.
#!/usr/bin/perl use warnings; use strict; open my $fh, '>', '1.log' or die $!; my $stdout = *STDOUT; my $stderr = *STDERR; *STDOUT = $fh; *STDERR = $fh; print "stdout 1\n"; warn "stderr 1\n"; *STDOUT = $stdout; *STDERR = $stderr; print "stdout 2\n"; warn "stderr 2\n";
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: STDERR Restore after redirect
by tultalk (Monk) on Apr 26, 2018 at 22:59 UTC

    Appreciate response.

    However, problem persists.

    The filehandle you're printing on got itself closed sometime before now. Check your control flow. print() on closed filehandle

    This makes no sense to me

      I blocked the call to sendmail and the handles were restored and the print command elsewhere executed.

      # SendMail(%mail) or die $Mail::sendMail::error;

      unblocking and

      #restore the file standard handles *STDOUT = $stdout; *STDERR = $stderr; close($fh); print "stdout 2\n";

      results in: print() on closed filehandle $fh at /home/abcus/public_html/httpsdocs/cgi-bin/lib/perl/manageusers.pm line 1124. stderr 2

      So sendMail call is causing problem

      ugh

        print() on closed filehandle $fh # ~~~

        But in the code, you just call print! So, $fh has been selected. The solution might be to

        select $stdout;
        after restoring stdout.

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        sendMail call is causing problem

        Yes, some minimal code to demo the problem. Fix is as choroba said.

        #!/usr/bin/perl use warnings; use strict; SendMemberMail(); print "stdout 2\n"; sub SendMemberMail { open my $fh, '>', '1.log' or die $!; MAIL: { local *STDOUT = $fh; local *STDERR = $fh; print "stdout 1\n"; warn "stderr 1\n"; # equivalent Mail:Sendmail code open S,'>','socket' or die; my $oldfh = select(S); $| = 1; select($oldfh); close S; } close $fh; # select(STDOUT); # uncomment to fix }
        poj

        ahem, poj called it when he said

        # equivalent Mail:Sendmail code open S,'>','socket' or die; my $oldfh = select(S); $| = 1; select($oldfh); close S;
        I saw it too when i looked at the SendMail code, he even used the same global filename and varlable that they did.

        notice he says

        # select(STDOUT); # uncomment to fix
        Did you try that?

        And notice he credits choroba for mentioning the fix first

        select $stdout;
        I am mentioning this AGAIN because i understand how frustrating it can be when "someone" skips the actual fix, and wanders away in all odd directions instead.

        The trouble comes from my($oldfh) = select(S); $| = 1; select($oldfh); at http://cpansearch.perl.org/src/ABELTJE/Test-Smoke-1.72/lib/inc/Mail/Sendmail.pm. Note that at the time of my($oldfh) = select(S); the selected default output (while maybe seeming to be *STDOUT) is infact $fh via *STDOUT, so that reselect at the end points directly to $fh rather than back at STDOUT as you might think at first thought. See restoring *STDOUT = $stdout; doesnt do much good when the default print file is directly set to $fh by the select.

        And just as a lark, seeing this

        unless ( socket S, AF_INET, SOCK_STREAM, (getprotobyname 'tcp')[2] ) { return fail("socket failed ($!)")
        brought back memories of doing things before i knew of use IO::Socket;, Hardly thought of in maybe 15 years, still works tho, well kinda. If you were to use low level sockets, (for some comparability reasons id suspect,) is there another way to unbuffer a file than to select it and set $|?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-25 18:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found