Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Capturing STDERR (this is not a FAQ)

by Tux (Abbot)
on Feb 10, 2015 at 07:15 UTC ( #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
Node Status?
node history
Node Type: note [id://1116149]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2020-02-20 08:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (86 votes). Check out past polls.

    Notices?