Hashes are more efficient to execute than this kind of explicit logic. Consider this untested code:
#!/usr/bin/perl -w use strict; use vars qw( $cmd_match %decomp_cmd %decomp_handler ); (scalar @ARGV) || die "Usage: $0 file(s)\n"; %decomp_cmd = ( '.tar.gz' => 'tar -zxvf $_', '.tgz' => 'tar -zxvf $_', '.bz2' => 'tar -Ixvf $_', '.z' => 'uncompress -dc $_| tar -xvf -', '.zip' => 'unzip $_', ); # Set up subs foreach (keys %decomp_cmd) { $decomp_handler{$_} = eval qq( sub { if (system "$decomp_cmd{$_}") { die("Cannot run '$decomp_cmd{$_}': $! (ret $?)\n"); } } ); } # Set up match { my $str = join "|", map {quotemeta($_)} keys %decomp_cmd; $cmd_match = qr/$str/; } foreach (@ARGV) { if (/($cmd_match)$/) { $decomp_handler{$1}->(); } else { warn("Don't know what to do with $_\n"); } }
Definitely overkill here. But you see the concept. It will perform quite well, is easy to extend, and moves all of the logic you should ever want to change into one place.

In reply to RE (tilly) 1: Expand your world by tilly
in thread Expand your world by jlp

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.