Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Contact Us
Online Help
Domain Status
Man Pages

Virtual Servers

Topology Map

Server Agreement
Year 2038

USA Flag



Man Pages

Manual Reference Pages  -  IO::CAPTURE::STDERR (3)

.ds Aq ’


"IO::Capture::Stderr" - Capture all output sent to "STDERR"



    use IO::Capture::Stderr;

    $capture = IO::Capture::Stderr->new();

    $capture->start();          # STDERR Output captured
    print STDERR "Test Line One\n";
    print STDERR "Test Line Two\n";
    print STDERR "Test Line Three\n";
    $capture->stop();           # STDERR output sent to wherever it was before start

    # In scalar context returns next line
    $line = $capture->read;
    print "$line";         # prints "Test Line One"

    $line = $capture->read;
    print "$line";         # prints "Test Line Two"

    # move line pointer to line 1

    $line = $capture->read;
    print "$line";         # prints "Test Line One"

    # Find out current line number
    $current_line_position = $capture->line_pointer;

    # In List Context return an array(list)
    @all_lines = $capture->read;

    # Example 1 - "Using in module tests"
    #  Note: If you dont want to make users install
    #        the IO::Capture module just for your tests,
    #        you can just install in the t/lib directory
    #        of your module and use the lib pragma in 
    #        your tests.

    use lib "t/lib";
    use IO::Capture:Stderr;

    use Test::More;

        # Create new capture object.  Showing FORCE_CAPTURE_WARN being cleared
        # for example, but 0 is the default, so you dont need to specify
        # unless you want to set.
    my $capture =  IO::Capture:Stderr->new( {FORCE_CAPTURE_WARN => 0} );

    # execute with a bad parameter to make sure get
    # an error.

    ok( ! $test("Bad Parameter") );



The module IO::Capture::Stderr, is derived from the abstract class IO::Capture. See IO::Capture. The purpose of the module (as the name suggests) is to capture any output sent to STDOUT. After the capture is stopped, the STDOUT filehandle will be reset to the previous location. E.g., If previously redirected to a file, when IO::Capture->stop is called, output will start going into that file again.

Note: This module won’t work with the perl function, system(), or any other operation
involving a fork(). If you want to capture the output from a system command,
it is faster to use open() or back-ticks.

       my $output = `/usr/sbin/ls -l 2>&1`;



o Creates a new capture object.
o An object can be reused as needed, so will only need to do one of these.
o Be aware, any data previously captured will be discarded if a new capture session is started.


o Start capturing data into the IO::Capture Object.
o Can <B>notB> be called on an object that is already capturing.
o Can <B>notB> be called while STDERR tied to an object.
o undef will be returned on an error.


o Stop capturing data and point STDERR back to it’s previous output location I.e., untie STDERR


o In Scalar Context
o Lines are read from the buffer at the position of the line_pointer, and the pointer is incremented by one.

    $next_line = $capture->read;

o In List Context
o The array is returned. The line_pointer is not affected.

    @buffer = $capture->read;

o Data lines are returned exactly as they were captured. You may want to use chomp on them if you don’t want the end of line character(s)

    while (my $line = $capture->read) {
        chomp $line;
        $cat_line = join , $cat_line, $line;


o Reads or sets the line_pointer.

    my $current_line = $capture->line_pointer;


Pass any arguments to new() in a single array reference.

   IO::Capture::Stderr->new( {FORCE_CAPTURE_WARN => 1} );


Normally, IO::Capture::Stderr will capture text from warn() function calls. This is because output from warn() is normally directed to STDERR. If you wish to force IO::Capture::Stderr to grab the text from warn(), set FORCE_CAPTURE_WARN to a 1. Then IO::Capture::Stderr will save the handle that $SIG{__WARN__} was set to, redirect it to itself on start(), and then set $SIG{__WARN__} back after stop() is called.


    Adding Features

If you would like to sub-class this module to add a feature (method) or two, here is a couple of easy steps. Also see IO::Capture::Overview.
1. Give your package a name

    package MyPackage;

2. Use this IO::Capture::Stderr as your base class like this:

    package MyPackage;

    use base qw/IO::Capture::Stderr/;

3. Add your new method like this

    package MyPackage;

    use base qw/IO::Capture::Stderr/;

    sub grep {
        my $self = shift;

        for $line (

See Also





Mark Reynolds

Jon Morgan


Copyright (c) 2003, Mark Reynolds. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.
Search for    or go to Top of page |  Section 3 |  Main Index

perl v5.20.3 IO::CAPTURE::STDERR (3) 2005-04-30

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.