The system() and exec() functions both execute system commands.
system() forks, executes the commands given in its arguments, waits for them to return, then allows your Perl script to continue. exec() does not fork, and exits when it's done. system() is by far the more commonly used.
% perl -we 'system("/bin/true"); print "Foo\n";' Foo % perl -we 'exec("/bin/true"); print "Foo\n";' Statement unlikely to be reached at -e line 1. (Maybe you meant system() when you said exec()?) |
If the system command fails, the error message will be available via the special variable $!.
% perl -e 'system("cat non-existant-file") || die "$!";' cat: non-existant-file: No such file or directory |
Write a script to ask the user for a username on the system, then perform the finger command to see information about that user. (Answer: exercises/answers/finger.pl)