how to get process id in perl

Hey guys,

how can i get process id of some particular command in perl?

example:

$cmd = "date; sleep 10; date&";
system($cmd);

How can i get process id of system command?

system() does something like this in bad perl code:

$pid=fork();
if ($pid==0)
{
       exec(''/usr/bin/sh my command goes here');
}
$retval=wait;
print  "$pid\n";  

$pid in this case is the pid of the child process, not the pid of the exec'ed process - the process actaully running the command.

exec returns nothing when successful.

This is why it is hard to get the process pid in a system call. I would simply change the command:

$cmd = "echo $$ > ./pidfile;date; sleep 10; date&";

./pidfile contains the pid of the child process.

thanks jim,

second code work for me.