Slide 1
Most trusted JOB oriented professional program
DevOps Certified Professional (DCP)

Take your first step into the world of DevOps with this course, which will help you to learn about the methodologies and tools used to develop, deploy, and operate high-quality software.

Slide 2
DevOps to DevSecOps – Learn the evolution
DevSecOps Certified Professional (DSOCP)

Learn to automate security into a fast-paced DevOps environment using various open-source tools and scripts.

Slide 2
Get certified in the new tech skill to rule the industry
Site Reliability Engineering (SRE) Certified Professional

A method of measuring and achieving reliability through engineering and operations work – developed by Google to manage services.

Slide 2
Master the art of DevOps
Master in DevOps Engineering (MDE)

Get enrolled for the most advanced and only course in the WORLD which can make you an expert and proficient Architect in DevOps, DevSecOps and Site Reliability Engineering (SRE) principles together.

Slide 2
Gain expertise and certified yourself
Azure DevOps Solutions Expert

Learn about the DevOps services available on Azure and how you can use them to make your workflow more efficient.

Slide 3
Learn and get certified
AWS Certified DevOps Professional

Learn about the DevOps services offered by AWS and how you can use them to make your workflow more efficient.

previous arrow
next arrow

Difference between exec and system in perl?

Spread the Knowledge

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

Rajesh Kumar
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