Difference between exec and system in perl?

scmuser created the topic: Difference between exec and system in perl?

What are the Difference between exec and system in perl? please explain….

rajeshkumar replied the topic: Re: Difference between exec and system in perl?

exec function in perl

The exec function executes a system command and never returns; use system instead of exec if you want it to return. It fails and returns false only if the command does not exist and it is executed directly instead of via your system’s command shell

Since it’s a common mistake to use exec instead of system, Perl warns you if there is a following statement that isn’t die, warn, or exit (if -w is set–but you always do that, right?). If you really want to follow an exec with some other statement, you can use one of these styles to avoid the warning:
exec (‘foo’) or print STDERR “couldn’t exec foo: $!”;
{ exec (‘foo’) }; print STDERR “couldn’t exec foo: $!”;

Example:
$shell = ‘/bin/csh’;
exec $shell ‘-sh’; # pretend it’s a login shell

or
exec {‘/bin/csh’} ‘-sh’; # pretend it’s a login shell

More..

Executes a system command (directly, not within a shell) and never returns to the calling script, except if the command specified does not exist and has been called directly, instead of indirectly through a shell. The operation works as follows:

* If there is only one scalar argument that contains no shell metacharacters, then the argument is converted into a list and the command is executed directly, without a shell.
* If there is only one scalar argument that contains shell metacharacters, then the argument is executed through the standard shell, usually /bin/sh on Unix.
* If LIST is more than one argument, or an array with more than one value, then the command is executed directly without the use of a shell.

Return Value: 0 only if the command specified cannot be executed

Example

Following are the usage…
exec ‘/bin/echo’, ‘Your arguments are: ‘, @ARGV;
exec “sort $outfile | uniq”;

Another example:
exec {‘/bin/csh’} ‘-sh’; # pretend it’s a login shell

System function in perl

Does exactly the same thing as exec LIST , except that a fork is done first, and the parent process waits for the child process to exit. Note that argument processing varies depending on the number of arguments. If there is more than one argument in LIST, or if LIST is an array with more than one value, starts the program given by the first element of the list with arguments given by the rest of the list.

Executes the command specified by PROGRAM, passing LIST as arguments to the command.The return value is the exit status of the program as returned by the wait function. To obtain the actual exit value, divide by 256.

Return Value : Exit status of program as returned by wait

Example

Try out following example:
#!/usr/bin/perl -w
system(“ls -F /var > /tmp/t.tmp”);

It will produce following result:

A file in /tmp direcory, check it out.

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x