Sometimes, you want a system(), but you want the child to have a different current directory, environment, Standard Input/Output/Error, or whatever normally gets inherited by a fork. The trick is to emulate the system() with slightly lower-level calls, and then insert your code for changes where only the child executes it. Here's a sample skeleton (from when I was answering a question).
# replacing system("date") with: defined(my $pid = fork) or die "Cannot fork: $!"; unless($pid) { # I'm the child # make your env changes here # you can also change directory, umask, priority, etc. exec "date"; die "date not found (just like me last friday night)"; } # parent continues here... { # to emulate system(), we need to ignore INT and QUIT # while the kid is foreground local $SIG{INT} = local $SIG{QUIT} = 'IGNORE'; waitpid($pid, 0); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: open-coding a system() operation
by Juerd (Abbot) on Apr 22, 2003 at 00:35 UTC | |
by merlyn (Sage) on Apr 22, 2003 at 02:40 UTC | |
by dug (Chaplain) on Apr 22, 2003 at 04:14 UTC | |
Re: open-coding a system() operation
by Anonymous Monk on Jan 02, 2004 at 19:10 UTC |
Back to
Cool Uses for Perl