MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

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

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Perl Punctuation and Sigils: Practical Symbol Reference with Examples

A clear reference to the punctuation marks you see most often in Perl code, including sigils, operators, delimiters, and quote forms. Includes usage tables, example snippets, and a quick decision flow for choosing the right sigil.

Read More

Perl Training for DevOps and Build Engineers: Updated Course Guide and Curriculum

This revised guide converts an older Perl training announcement into a practical reference for engineering teams. It summarizes the course format, curriculum, tooling, lab examples, and enrollment details that should be validated before booking.

Read More

Perl Interview Questions for DevOps Engineers: Practical Concepts, Commands, and Clear Answers

A cleaned-up, interview-focused Perl guide for DevOps and SRE roles. It covers one-liners, scope rules, references, regex, module loading, and command execution with practical examples.

Read More

Executing External Commands in Perl: system, exec, backticks, and open Explained

Perl provides multiple ways to run external commands, and each one behaves differently. This guide shows when to use system, exec, backticks, or open, with safe patterns for output capture and exit-code handling.

Read More

Perl use vs require: Compile-Time and Runtime Module Loading Explained

In Perl, `use` and `require` both load code, but they do it at different times and with different side effects. This guide shows when each is appropriate, how imports work, and patterns that avoid common production bugs.

Read More

Perl File Test Operators Explained for DevOps Automation

Perl’s built-in file test operators let you check existence, type, size, permissions, and ownership without external commands. This guide explains the operators, real vs effective ID checks, and production-friendly patterns for automation scripts.

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x