<?xml version="1.0" encoding="windows-1252"?>
<node id="557107" title="Modules which improve system() ?" created="2006-06-23 03:51:25" updated="2006-06-22 23:51:25">
<type id="115">
perlquestion</type>
<author id="116014">
pjf</author>
<data>
<field name="doctext">
&lt;p&gt;Ahoy there monks,
&lt;/p&gt;&lt;p&gt;
I'm writing some material which is primarily aimed at system administrators who'd like to use Perl to make their life easier.  Part of that discussion involves demonstrating how to use &lt;tt&gt;system()&lt;/tt&gt; to call external programs, but unfortunately I've hit a snag.
&lt;/p&gt;&lt;p&gt;
Checking &lt;tt&gt;system()&lt;/tt&gt; to see if anything has gone wrong is a pain.  Our command may not actually get run, in which case &lt;tt&gt;$? == -1&lt;/tt&gt;, or it may have been killed by a signal, in which case &lt;tt&gt;WIFSIGNALED($?)&lt;/tt&gt; will be true.  It may have run to completion, but the exit code may not have been something that we expect.
&lt;/p&gt;&lt;p&gt;
Handling the error status from system is always a pain, even in the most simpliest case:
&lt;/p&gt;
&lt;blockquote&gt;&lt;tt&gt;
system("some_command") and die "...";
&lt;/tt&gt;&lt;/blockquote&gt;
&lt;p&gt;
Some less experienced programmers may see the &lt;i&gt;and die&lt;/i&gt; and think it is a mistake, since for all other Perl built-ins one would use &lt;i&gt;or die&lt;/i&gt;.  Even the experienced programmers would be hard-pressed to say what should actually go in the die message.  Surely &lt;tt&gt;$!&lt;/tt&gt; should be reported if &lt;tt&gt;$? == -1&lt;/tt&gt;, but otherwise one ends up with an awkward series of statements to unpack signals and exit status.  And what if our requirements change later on, and it's okay if &lt;tt&gt;some_command&lt;/tt&gt; also returns an exit status of &lt;tt&gt;1&lt;/tt&gt;?  Surely there must be a better way?
&lt;/p&gt;&lt;p&gt;
Unfortunately, I can't seem to find a Perl module that makes this &lt;i&gt;easy&lt;/i&gt;.  I'd like to be able to write code that reads:
&lt;/p&gt;
&lt;blockquote&gt;&lt;tt&gt;
use Some::Module qw(run);&lt;br /&gt;
&lt;br /&gt;
run("some_command");
&lt;/tt&gt;&lt;/blockquote&gt;
&lt;p&gt;
and have &lt;tt&gt;run&lt;/tt&gt; throw an appropriately verbose exception if the command doesn't start, gets zapped by a signal, or returns non-zero.  If I later need to allow my command to return a couple of different status values (say 0, 1 and 5), then I can change my code to be:
&lt;/p&gt;
&lt;blockquote&gt;&lt;tt&gt;
my $exit_value = run( &amp;#91;0,1,5&amp;#93;, "some_command");
&lt;/tt&gt;&lt;/blockquote&gt;
&lt;p&gt;
and be &lt;i&gt;certain&lt;/i&gt; that &lt;tt&gt;$exit_value&lt;/tt&gt; will be either 0, 1 or 5, or an exception will be thrown.
&lt;/p&gt;&lt;p&gt;
Have I been missing this obviously simple module all this time, and if so, what is it called?  (If not, then watch this space for a new module announcement.)
&lt;/p&gt;&lt;p&gt;
Many thanks,
&lt;/p&gt;&lt;p&gt;
&lt;b&gt;Update:&lt;/b&gt; You can find my solution to the problem using &lt;a href="http://search.cpan.org/perldoc?IPC::System::Simple"&gt;IPC::System::Simple&lt;/a&gt;.
&lt;/p&gt;
&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-116014"&gt;
&lt;i&gt;Paul Fenwick&lt;/i&gt;&lt;br&gt;
&lt;a href="http://perltraining.com.au/"&gt;Perl Training Australia&lt;/a&gt;
&lt;/div&gt;&lt;/div&gt;</field>
</data>
</node>
