<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Execute external commands Archives - scmGalaxy</title>
	<atom:link href="https://www.scmgalaxy.com/tutorials/tag/execute-external-commands/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.scmgalaxy.com/tutorials/tag/execute-external-commands/</link>
	<description>A Platform for Software Engineers</description>
	<lastBuildDate>Sat, 27 Feb 2021 09:52:53 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1</generator>
	<item>
		<title>How to Execute external commands by using perl?</title>
		<link>https://www.scmgalaxy.com/tutorials/executing-external-commands-using-perl/</link>
					<comments>https://www.scmgalaxy.com/tutorials/executing-external-commands-using-perl/#comments</comments>
		
		<dc:creator><![CDATA[scmgalaxy K]]></dc:creator>
		<pubDate>Fri, 04 May 2012 11:02:25 +0000</pubDate>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[commands]]></category>
		<category><![CDATA[Execute]]></category>
		<category><![CDATA[Execute external commands]]></category>
		<category><![CDATA[Execute external commands from Perl]]></category>
		<category><![CDATA[External]]></category>
		<category><![CDATA[External commands]]></category>
		<category><![CDATA[Guide]]></category>
		<category><![CDATA[How]]></category>
		<category><![CDATA[Implement]]></category>
		<category><![CDATA[Implement external commands by using perl]]></category>
		<category><![CDATA[Perl Guide for executing external commands]]></category>
		<category><![CDATA[Process]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Using]]></category>
		<category><![CDATA[Ways]]></category>
		<guid isPermaLink="false">http://www.scmgalaxy.com/tutorials/2012/05/04/executing-external-commands-using-perl/</guid>

					<description><![CDATA[<p>There are many ways to execute external commands from Perl. The most commons are: system function exec function backticks (&#8220;) [&#8230;]</p>
<p>The post <a href="https://www.scmgalaxy.com/tutorials/executing-external-commands-using-perl/">How to Execute external commands by using perl?</a> appeared first on <a href="https://www.scmgalaxy.com/tutorials">scmGalaxy</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-3828 aligncenter" src="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2012/05/external-commands-using-per.png" alt="execute-external-commands-from-perl" width="600" height="400" srcset="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2012/05/external-commands-using-per.png 600w, https://www.scmgalaxy.com/tutorials/wp-content/uploads/2012/05/external-commands-using-per-300x200.png 300w" sizes="(max-width: 600px) 100vw, 600px" /></p>
<p>There are many ways to execute external commands from Perl. The most commons are:</p>
<ul type="disc">
<li><strong>system</strong> function</li>
<li><strong>exec</strong> function</li>
<li><strong>backticks (&#8220;)</strong> operator</li>
<li><strong>open</strong> function</li>
</ul>
<p>All of these methods have different behaviour, so you should choose which one to use depending of your particular need. In brief, these are the recommendations:</p>
<table border="1" cellpadding="0">
<tbody>
<tr>
<td><strong>method</strong></td>
<td><strong>use if &#8230;</strong></td>
</tr>
<tr>
<td>system()</td>
<td>you want to execute a command and don&#8217;t want to capture its output</td>
</tr>
<tr>
<td>exec</td>
<td>you don&#8217;t want to return to the calling perl script</td>
</tr>
<tr>
<td>backticks</td>
<td>you want to capture the output of the command</td>
</tr>
<tr>
<td>open</td>
<td>you want to pipe the command (as input or output) to your script</td>
</tr>
</tbody>
</table>
<p>More detailed explanations of each method follows:</p>
<p><strong>Using system() </strong><br />
system() executes the command specified. It doesn&#8217;t capture the output of the command.<br />
system() accepts as argument either a scalar or an array. If the argument is a scalar, system() uses a shell to execute the command (&#8220;/bin/sh -c command&#8221;); if the argument is an array it executes the command directly, considering the first element of the array as the command name and the remaining array elements as arguments to the command to be executed.<br />
For that reason, it&#8217;s highly recommended for efficiency and safety reasons (specially if you&#8217;re running a cgi script) that you use an array to pass arguments to system()</p>
<pre><strong>Example: </strong>     #-- calling 'command' with  arguments    system("command arg1 arg2 arg3");         #-- better way of calling the same command    system("command", "arg1", "arg2",  "arg3");     The return value  is set in <strong>$?</strong>; this value is the exit status of the command as returned  by the 'wait' call; to get the real exit status of the command you have to  shift right by 8 the value of $? ($? &gt;&gt; 8).     If the value of <strong>$?</strong> is -1, then the command failed to execute, in that case you may check the value  of <strong>$!</strong> for the reason of the failure.</pre>
<pre><strong>Example: </strong>     system("command",  "arg1");    if ( $? == -1 )    {      print "command failed: $!\n";    }    else    {      printf "command exited with value %d", $? &gt;&gt; 8;    }</pre>
<p><strong>Using exec() </strong><br />
The exec() function executes the command specified and never returns to the calling program, except in the case of failure because the specified command does not exist AND the exec argument is an array.<br />
Like in system(), is recommended to pass the arguments of the functions as an array.</p>
<p><strong>Using backticks (&#8220;) </strong><br />
In this case the command to be executed is surrounded by backticks. The command is executed and the output of the command is returned to the calling script.<br />
In scalar context it returns a single (possibly multiline) string, in list context it returns a list of lines or an empty list if the command failed.<br />
The exit status of the executed command is stored in <strong>$?</strong> (see system() above for details).</p>
<pre><strong>Example: </strong>     #-- scalar context    $result = `command arg1 arg2`;         #-- the same command in list context    @result = `command arg2 arg2`;     Notice that the  only output captured is STDOUT, to collect messages sent to STDERR you should  redirect STDERR to STDOUT</pre>
<pre><strong>Example: </strong>     #-- capture STDERR as well  as STDOUT    $result = `command 2&gt;&amp;1`;</pre>
<p><strong>Using open() </strong><br />
Use open() when you want to:<br />
&#8211; capture the data of a command (syntax: open(&#8220;command |&#8221;))<br />
&#8211; feed an external command with data generated from the Perl script (syntax: open(&#8220;| command&#8221;))</p>
<pre><strong>Examples: </strong>     #-- list the processes  running on your system    open(PS,"ps -e -o pid,stime,args |") || die "Failed: $!\n";    while ( &lt;PS&gt; )    {      #-- do something here    }         #-- send an email to user@localhost    open(MAIL, "| /bin/mailx -s test user\@localhost ") || die  "mailx failed: $!\n";  print MAIL "This is a test message";</pre>
<p>The post <a href="https://www.scmgalaxy.com/tutorials/executing-external-commands-using-perl/">How to Execute external commands by using perl?</a> appeared first on <a href="https://www.scmgalaxy.com/tutorials">scmGalaxy</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.scmgalaxy.com/tutorials/executing-external-commands-using-perl/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
