From b9956e388403d697c7daf51563f5a5f1bf5b111d Mon Sep 17 00:00:00 2001 From: Thierry Vignaud Date: Sun, 23 Nov 2003 21:33:02 +0000 Subject: introduce bg_command object that enable to fork a sub that give back data through STDOUT a la run_program::get_stdout but w/ arbitrary perl code instead of external program and in background --- perl-install/run_program.pm | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'perl-install') diff --git a/perl-install/run_program.pm b/perl-install/run_program.pm index 4ebce7249..0a53ffa39 100644 --- a/perl-install/run_program.pm +++ b/perl-install/run_program.pm @@ -127,3 +127,26 @@ sub raw { } } + +# run in background a sub that give back data through STDOUT a la run_program::get_stdout but w/ arbitrary perl code instead of external program +package bg_command; + +sub new { + my ($class, $sub) = @_; + my $o = bless {}, $class; + if ($o->{pid} = open(my $fd, "-|")) { + $o->{fd} = $fd; + $o; + } else { + $sub->(); + c::_exit(0); + } +} + +sub DESTROY { + my ($o) = @_; + close $o->{fd} or warn "kid exited $?"; + waitpid $o->{pid}, 0; +} + +1; -- cgit v1.2.1