Proc::SyncExec3pm
Spawn processes but report exec() errors
Usage
# Normal-looking piped opens which properly report exec() errors in $!:sync_open WRITER_FH, "|command -with args" or die $!;sync_open READER_FH, "command -with args|" or die $!;# Synchronized fork/exec which reports exec errors in $!:$pid = sync_exec $command, @arg;$pid = sync_exec $code_ref, $cmd, @arg; # run code after fork in kid# fork() which retries if it fails, then croaks() if it still fails.$pid = fork_retry;$pid = fork_retry 100; # retry 100 times rather than 5$pid = fork_retry 100, 2; # sleep 2 rather than 5 seconds between# A couple of interfaces similar to sync_open() but which let you# avoid the shell:$pid = sync_fhpopen_noshell READERFH, 'r', @command;$pid = sync_fhpopen_noshell WRITERFH, 'w', @command;$fh = sync_popen_noshell 'r', @command_which_outputs;$fh = sync_popen_noshell 'w', @command_which_inputs;($fh, $pid) = sync_popen_noshell 'r', @command_which_outputs;($fh, $pid)= sync_popen_noshell 'w', @command_which_inputs;
Overview
This module contains functions for synchronized process spawning with full error return. If the child's exec() call fails the reason for the failure is reported back to the parent.
These functions will croak() if they encounter an unexpected system error, such as a pipe() failure or a repeated fork() failure.
Nothing is exported by default.
fork_retry[max-retries [sleep-between]]-
This function runs
fork()until it succeeds or until max-retries (default 5) attempts have been made, sleeping sleep-between seconds (default 5) between attempts. If the lastfork()failsfork_retrycroak()s.
sync_exec[code] command...-
This function is similar to a
fork()/exec()sequence but with a few twists.sync_execdoes not return until after thefork()ed child has already performed itsexec(). The synchronization this provides is useful in some unusual circumstances.Normally the pid of the child process is returned. However, if the child fails its
exec()sync_execreturns undef and sets $! to the reason for the child'sexec()failure.Since the
@cmdarray is passed directly to Perl'sexec()Perl might choose to invoke the command via the shell if@cmdcontains only one element and it looks like it needs a shell to interpret it. If this happens the return value ofsync_execonly indicates whether theexec()of the shell worked.The optional initial code argument must be a code reference. If it is present it is run in the child just before
exec()is called. You can use this to set up redirections or whatever. If code returns false no exec is performed, instead a failure is returned using the current $! value (or EINTR if $! is 0).If the
fork()fails or if there is some other unexpected system errorsync_execcroak()s rather than returning.
sync_fhpopen_noshellfh type cmd [arg]...-
This is a
popen()but it never invokes the shell and it usessync_exec()under the covers. See "sync_exec".The type is either
'r'to read from the process or'w'to write to it.The return value is the pid of the forked process.
sync_popen_noshelltype cmd arg...-
This is like
sync_fhpopen_noshell, but you don't have to supply the filehandle.If called in an array context the return value is a list consisting of the filehandle and the PID of the child. In a scalar context only the filehandle is returned.
sync_openfh [open-spec]-
This is like a Perl
open()except that if a pipe is involved and the impliedexec()failssync_open()fails with $! set appropriately. See "sync_exec".Like
sync_exec,sync_opencroak()s if there is an unexpected system error (such as a failedpipe()).Also like
sync_exec, if you use a command which Perl needs to use the shell to interpret you'll only know if the exec of the shell worked. Usesync_fhpopen_noshellorsync_execto be sure that this doesn't happen.
Releases
13 releases since Debian 2.2 (Potato); the text changed 6 times.