Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Best Way to Redirect STDERR to a Scalar

by Roger (Parson)
on Sep 14, 2003 at 03:05 UTC ( #291338=note: print w/ replies, xml ) Need Help??


in reply to Best Way to Redirect STDERR to a Scalar

The IO::Scalar module is one way to redirect STDERR to a scalar. There is another method for redirecting STDERR by using the IO::Capture module, and more specifically, by using the IO::Capture::Stderr module.

use IO::Capture::Stderr; my $capture = new IO::Capture::Stderr; $capture->start(); # ... print anything to STDERR ... print STDERR "Test 1\n"; print STDERR "Test 2\n"; $capture->stop(); # To map everything into an array -- my @messages = $capture->read;
The CPAN site offers detailed description of this module.


Comment on Re: Best Way to Redirect STDERR to a Scalar
Download Code
Re: Re: Best Way to Redirect STDERR to a Scalar
by shenme (Priest) on Sep 14, 2003 at 04:23 UTC
    While attractive, this is just one more way of tieing the STDERR glob.   Because this nor any of the other tie methods 'really' hook into the filehandle used by STDERR they don't help capture the low-level references to STDERR (aka fileno(STDERR)==2) done by code like Devel::Peek.

    Tested with the following code it fails the "warn test" and testing versus Mr. Muskrat's target Devel::Peek:

    use IO::Capture::Stderr; use Devel::Peek; my $data; print STDERR " STDERR test (1)\n"; my $capture = IO::Capture::Stderr->new(); $capture->start(); print STDERR " STDERR test (2)\n"; warn " STDERR test (2a)"; my $test = 'string'; Dump($test); $capture->stop(); print STDERR " STDERR test (3)\n"; my @the_output = $capture->read; if( @the_output ) { foreach (@the_output) { print " >>> ", $_; } }
    produces output
     STDERR test (1)
     STDERR test (2a) at mrmskrat4.pl line 15.
    SV = PV(0x183ec0c) at 0x197138c
      REFCNT = 1
      FLAGS = (PADBUSY,PADMY,POK,pPOK)
      PV = 0x196deb4 "string"\0
      CUR = 6
      LEN = 7
     STDERR test (3)
      >>>  STDERR test (2)
    

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2014-08-22 02:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (145 votes), past polls