#!/usr/bin/perl use strict; use warnings; my $pid=fork(); defined($pid) or die "Can't fork: $!"; if ($pid) { # parent print "Parent waiting for the child ...\n"; waitpid($pid,0); print "Child has exited, exit code = $?\n"; } else { # child chdir('/tmp') or die "Can't chdir to /tmp: $!"; %ENV=(); # completely clear environment, for shorter demo output, maybe also for security $ENV{'PATH'}='/bin:/usr/bin'; $ENV{'FOO'}='foo fofoo fofofooo'; $ENV{'BAR'}='bar bar barrrrrrrrrk'; exec($^X,'-e','print "* Hello from a new perl\n";print `pwd`;print "* $_=$ENV{$_}\n" for sort keys %ENV; print "* bye\n"') or die "Can't execute $^X: $!"; # not reached } #### >perl envchild.pl Parent waiting for the child ... * Hello from a new perl /tmp * BAR=bar bar barrrrrrrrrk * FOO=foo fofoo fofofooo * PATH=/bin:/usr/bin * bye Child has exited, exit code = 0 >