IPC::Run::SafeHandles - Use IPC::Run and IPC::Run3 safely


IPC-Run-SafeHandles documentation Contained in the IPC-Run-SafeHandles distribution.

Index


Code Index:

NAME

Top

IPC::Run::SafeHandles - Use IPC::Run and IPC::Run3 safely

SYNOPSIS

Top

    use IPC::Run::SafeHandles;

DESCRIPTION

Top

IPC::Run and IPC::Run3 are both very upset when you try to use them under environments where you have STDOUT and/or STDERR tied to something else, such as under fastcgi.

The module adds safe-guarding code when you call IPC::Run or IPC::Run3 under such environment to make sure it always works.

If you intend to release your code to work under normal envionrment as well as under fastcgi, simply use this module after the IPC modules are loaded in your code.

unimport

When unimport, the original IPC::Run and/or IPC::Run3 functions are restored.

AUTHOR

Top

Chia-liang Kao, <clkao at bestpractical.com>

BUGS

Top

Please report any bugs or feature requests to bug-ipc-run-safehandles at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IPC-Run-SafeHandles. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc IPC::Run::SafeHandles

You can also look for information at:

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/IPC-Run-SafeHandles

* CPAN Ratings

http://cpanratings.perl.org/d/IPC-Run-SafeHandles

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=IPC-Run-SafeHandles

* Search CPAN

http://search.cpan.org/dist/IPC-Run-SafeHandles

ACKNOWLEDGEMENTS

Top

COPYRIGHT & LICENSE

Top


IPC-Run-SafeHandles documentation Contained in the IPC-Run-SafeHandles distribution.
package IPC::Run::SafeHandles;

use warnings;
use strict;
use IO::Handle ();

our $VERSION = '0.02';

my $wrapper_context = [];

sub _wrap_it {
    no strict 'refs';
    my $typeglob = shift;
    my $caller = shift;

    my $original = *$typeglob{CODE};
    my $unwrap = 0;

    my $wrapper = sub {

        goto &$original unless $ENV{FCGI_ROLE};

	my $stdout = IO::Handle->new;
	$stdout->fdopen( 1, 'w' );
	local *STDOUT = $stdout;

	my $stderr = IO::Handle->new;
	$stderr->fdopen( 2, 'w' );
	local *STDERR = $stderr;
	$original->(@_);
    };
    no warnings 'redefine';
    my $callerglob = $typeglob; $callerglob =~ s/IPC::Run3?/$caller/;
    *{$typeglob} = $wrapper;
    *{$callerglob} = $wrapper;
    push @$wrapper_context,
	bless(sub { no warnings 'redefine';
                    *{$callerglob} = $original;
		    *{$typeglob} = $original }, __PACKAGE__);
}

sub import {
    my $caller = caller();
    _wrap_it('IPC::Run::run', $caller)   if $INC{'IPC/Run.pm'};
    _wrap_it('IPC::Run3::run3', $caller) if $INC{'IPC/Run3.pm'};

    unless (@$wrapper_context) {
	Carp::carp "Use of IPC::Run::SafeHandles without using IPC::Run or IPC::Run3 first";
    }
}

sub unimport {
    $wrapper_context = [];
}

sub DESTROY { $_[0]->() }

1; # End of IPC::Run::SafeHandles