I'm writing a low-level wrapper for backticks so that shelling out to run a command doesn't hang or kill my daemon. I'd like to be able to drop it in wherever I am using backticks without having to make any changes (beyond replacing the backticks with a subroutine call). By low-level I mean that I would prefer to have to do as little as possible within the wrapper -- e.g. as little error handling as possible (so even $@ would be handled outside of the wrapper). Does the code below fit the bill? And more importantly, is there a way to make it look prettier (e.g. reduce duplicate code)?

sub cmd_wrapper { my $cmd = shift; # CASE: Called in list context if( wantarray() ) { my @out; eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm 15; # Give the command 15 seconds to return @out = `$cmd`; alarm 0; }; return @out; } # CASE: Called in scalar context else { my $out; eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm 15; # Give the command 15 seconds to return $out = `$cmd`; alarm 0; }; return $out; } }

Elda Taluta; Sarks Sark; Ark Arks


In reply to Is there a way to clean up this backticks wrapper? by Argel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.