Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Best Way to Redirect STDERR to a Scalar

by Mr. Muskrat (Abbot)
on Sep 14, 2003 at 01:35 UTC ( #291330=note: print w/ replies, xml ) Need Help??


in reply to Best Way to Redirect STDERR to a Scalar

I'm trying to capture the output of Devel::Peek's Dump function which outputs to STDERR. The methods outlined in my original post work fine.

I just want to know if there is a better way.

I've tried IO::Scalar (see above).

I've tried Tie::Handle::Scalar. It works but using it results in a warning 'Permission denied at C:/Perl/site/lib/Tie/Handle/Scalar.pm line 114.' I looked and that is the line that deletes the temporary file. I read further and I see that File::Temp provides a much safer means of using temporary files so T::H::S is out.

Tie::STDERR is out mainly because I don't want to capture all messages sent to STDERR, only those generated by D::P's Dump function as mentioned above.

I'm going to be trying IO::String & IO::Stringy later this evening.

Any suggestions would be appreciated. Thank you.


Comment on Re: Best Way to Redirect STDERR to a Scalar
Re: Re: Best Way to Redirect STDERR to a Scalar
by shenme (Priest) on Sep 14, 2003 at 02:03 UTC
    Okey-dokey, I can understand frustration with packages that don't allow you to (easily) capture their debugging output.   Please try the following which works on ActiveState 5.6.1 build 635 running on Windows XP.   Amazing, no open's, no close's, but all TIE'd up prettily anyway.
    my $data; print STDERR " STDERR test (1)\n"; tie *STDERR, 'IO::Scalar', \$data; print STDERR " STDERR test (2)\n"; untie *STDERR; print STDERR " STDERR test (3)\n"; printf " Scalar string has length %d\n", length($data); printf " and contains '%s'\n", $data;
    Outputs
     STDERR test (1)
     STDERR test (3)
      Scalar string has length 17
        and contains ' STDERR test (2)
    '
    

    Update: Drat, it works wonderfully until you hit submit, then you think of the case that doesn't work.   If I throw in a warn which presumably uses the filehandle more directly:

    tie *STDERR, 'IO::Scalar', \$data; print STDERR " STDERR test (2)\n"; warn " STDERR test (2a)"; untie *STDERR;
    I then see
     STDERR test (1)
     STDERR test (2a) at mrmskrat3.pl line 14.
     STDERR test (3)
      Scalar string has length 17
        and contains ' STDERR test (2)
    '
    
    showing that some output to 'STDERR' isn't going to be captured this way.   So it will very much depend on the way the package outputs to STDERR whether this will work.   Rats!

      I really thought this would do it but it did not. :-(

      use IO::Scalar; use Devel::Peek; print STDERR " STDERR test (1)\n"; my $io = ioscalar(); print STDERR " STDERR test (3)\n"; printf " Scalar string has length %d\n", length($io); printf " and contains '%s'\n", $io; print STDERR " STDERR test (4)\n"; my $peek = peek(); print STDERR " STDERR test (5)\n"; printf " Scalar string has length %d\n", length($peek); printf " and contains '%s'\n", $peek; sub ioscalar { my $data; tie *STDERR, 'IO::Scalar', \$data; print STDERR " STDERR test (2)\n"; untie *STDERR; return $data; } sub peek { my $data; my $test = 'string'; tie *STDERR, 'IO::Scalar', \$data; Dump($test); untie *STDERR; return $data; } __DATA__ STDERR test (1) STDERR test (3) Scalar string has length 17 and contains ' STDERR test (2) ' STDERR test (4) SV = PV(0x177eb58) at 0x1cbc540 REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x1773c74 "string"\0 CUR = 6 LEN = 7 STDERR test (5) Scalar string has length 0 and contains ''

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2014-08-20 05:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (105 votes), past polls