[Israel.pm] using "system"

Mikhael Goikhman migo at homemail.com
Sun Oct 31 03:28:30 PST 2004

On 31 Oct 2004 12:31:04 +0200, Offer Kaye wrote:
> Can someone explain to me the difference between using:
>    system LIST
> and
>    system PROGRAM LIST
> An example of each form would be nice...

Here is an example of both:

  system("ls", "-al");

  system {"ls"} "dir", "-al";

If you know C and libc, the first command calls something like:

  execvp("ls", [ "ls", "-al" ]);

(Not really, C systax for arrays is different, but you get the point.)
And the second command calls:

  execvp("ls", [ "dir", "-al" ]);

On many systems, this causes "ps" to show "dir" and not "ls", although
actually what is run is /bin/ls.

And /bin/ls itself gets argv[0] that is "ls" (the first example), or
"dir" (the second example), and it may do different things then.

> Also, in "perldoc -f  exec", there are the following usage examples:
>    exec {'/bin/csh'} '-sh';
> and
>    exec { $args[0] } @args;
> Can anyone explain these examples, what do they do and how?

This is the same syntactic sugar for the same thing; the difference is
that with system, perl first forks and then calls execvp, and with exec,
it calls execvp directly and never returns on success.

"-" in "-sh" is a convention for labeling the login shell. So, your
example actually runs csh, but pretends it is login Bourne shell, then
"top" and "ps" will show "-sh".  When bash sees "sh" in argv[0], it
may probably activate some sh-simulation mode.


perl -e 'print+chr(64+hex)for+split//,d9b815c07f9b8d1e'

More information about the Perl mailing list