{"id":318,"date":"2012-05-04T11:02:25","date_gmt":"2012-05-04T11:02:25","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/2012\/05\/04\/executing-external-commands-using-perl\/"},"modified":"2017-12-25T04:44:15","modified_gmt":"2017-12-25T04:44:15","slug":"executing-external-commands-using-perl","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/executing-external-commands-using-perl\/","title":{"rendered":"How to Execute external commands by using perl?"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3828 aligncenter\" src=\"http:\/\/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.devopsschool.com\/blog\/wp-content\/uploads\/2012\/05\/external-commands-using-per.png 600w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2012\/05\/external-commands-using-per-300x200.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>There are many ways to execute external commands from Perl. The most commons are:<\/p>\n<ul type=\"disc\">\n<li><strong>system<\/strong> function<\/li>\n<li><strong>exec<\/strong> function<\/li>\n<li><strong>backticks (&#8220;)<\/strong> operator<\/li>\n<li><strong>open<\/strong> function<\/li>\n<\/ul>\n<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>\n<table border=\"1\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td><strong>method<\/strong><\/td>\n<td><strong>use if &#8230;<\/strong><\/td>\n<\/tr>\n<tr>\n<td>system()<\/td>\n<td>you want to execute a command and don&#8217;t want to capture its output<\/td>\n<\/tr>\n<tr>\n<td>exec<\/td>\n<td>you don&#8217;t want to return to the calling perl script<\/td>\n<\/tr>\n<tr>\n<td>backticks<\/td>\n<td>you want to capture the output of the command<\/td>\n<\/tr>\n<tr>\n<td>open<\/td>\n<td>you want to pipe the command (as input or output) to your script<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>More detailed explanations of each method follows:<\/p>\n<p><strong>Using system() <\/strong><br \/>\nsystem() executes the command specified. It doesn&#8217;t capture the output of the command.<br \/>\nsystem() 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 \/>\nFor 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>\n<pre><strong>Example: <\/strong>     #-- calling 'command' with  arguments    system(\"command arg1 arg2 arg3\");    \u00a0    #-- 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>\n<pre><strong>Example: <\/strong>     system(\"command\",  \"arg1\");    if ( $? == -1 )    {    \u00a0\u00a0print \"command failed: $!\\n\";    }    else    {    \u00a0\u00a0printf \"command exited with value %d\", $? &gt;&gt; 8;    }<\/pre>\n<p><strong>Using exec() <\/strong><br \/>\nThe 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 \/>\nLike in system(), is recommended to pass the arguments of the functions as an array.<\/p>\n<p><strong>Using backticks (&#8220;) <\/strong><br \/>\nIn 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 \/>\nIn 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 \/>\nThe exit status of the executed command is stored in <strong>$?<\/strong> (see system() above for details).<\/p>\n<pre><strong>Example: <\/strong>     #-- scalar context    $result = `command arg1 arg2`;    \u00a0    #-- 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>\n<pre><strong>Example: <\/strong>     #-- capture STDERR as well  as STDOUT    $result = `command 2&gt;&amp;1`;<\/pre>\n<p><strong>Using open() <\/strong><br \/>\nUse open() when you want to:<br \/>\n&#8211; capture the data of a command (syntax: open(&#8220;command |&#8221;))<br \/>\n&#8211; feed an external command with data generated from the Perl script (syntax: open(&#8220;| command&#8221;))<\/p>\n<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; )    {    \u00a0\u00a0#-- do something here    }    \u00a0    #-- 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>\n","protected":false},"excerpt":{"rendered":"<p>There are many ways to execute external commands from Perl. The most commons are: system function exec function backticks (&#8220;) operator open function 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: method use if &#8230; system() you&#8230;<\/p>\n","protected":false},"author":1,"featured_media":3828,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[14],"tags":[378,1670,2527,2530,2526,2528,1122,545,1467,2529,175,2531,593,213,1927,1452],"class_list":["post-318","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-perl","tag-commands","tag-execute","tag-execute-external-commands","tag-execute-external-commands-from-perl","tag-external","tag-external-commands","tag-guide","tag-how","tag-implement","tag-implement-external-commands-by-using-perl","tag-perl","tag-perl-guide-for-executing-external-commands","tag-process","tag-tutorial","tag-using","tag-ways"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=318"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/318\/revisions"}],"predecessor-version":[{"id":3829,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/318\/revisions\/3829"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/3828"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}