Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Capturing STDERR (this is not a FAQ)

by Tux (Canon)
on Feb 10, 2015 at 07:15 UTC ( [id://1116149]=note: print w/replies, xml ) Need Help??


in reply to Capturing STDERR (this is not a FAQ)

Preview != create :(

Capture::Tiny is indeed the way to go, with a small gotcha:

$ cat test.pl use 5.18.2; use warnings; use Capture::Tiny "capture"; my ($out, $err, $ext) = capture { # Local scope print STDERR "Output to STDERR\n"; warn "Warn!\n"; system "ls /tmp/bjfgerfbv"; qx{ls /tmp/jhgbejgrbe}; }; # Previous scope print STDERR "Hello world\n"; system "ls /tmp/foobar2"; print STDERR "OUT: $_\n" for split m/\n/ => $out; print STDERR "ERR: $_\n" for split m/\n/ => $err; $ perl test.pl Hello world /bin/ls: cannot access /tmp/foobar2: No such file or directory ERR: Output to STDERR ERR: Warn! ERR: /bin/ls: cannot access /tmp/bjfgerfbv: No such file or directory ERR: /bin/ls: cannot access /tmp/jhgbejgrbe: No such file or directory

The gotcha is that STDERR inside capture is not unbuffered, so if your last print STDERR "foo"; does not have a trailing newline, it is lost. That might change in a newer release (at least I hope)


Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-18 01:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found