So I've just gotten back into Perl and I've written a tarring utility for my first application. It seems to work okay, but I'm wondering how it could better be written. Any ideas?

   #!/usr/bin/perl -w
   use strict;
   use Archive::Tar;
   use Getopt::Std;
   use feature qw(say);

   my %opt;

   getopts('hcel', \%opt);

   help() if $opt{h};
   list() if $opt{l};
   extract() if $opt{e};
   compress() if $opt{c};

   sub help {
        say "Easily create and extract tarballs";
        say "  -h display this menu";
        say "  -c compress file/directory";
        say "     | eztar -c <file name you wish to make>
   <file/directory you wish to compress>";
        say "  -e extract tarball";
        say "     | eztar -e <tarball>";
        say "  -l list contents of tarball";
        say "     | eztar -l <tarball>";
   }

   sub list {
        if (defined $ARGV[0]) {
            my $tar = Archive::Tar->new();
   for($tar->list_archive($ARGV[0])) {
                say $_;
                exit 0;
            }
        } else {
            die 'No file provided.'
        }
   }

   sub extract {
        if (defined $ARGV[0]) {
            my $tar = Archive::Tar->new();
   $tar->read($ARGV[0]);
            $tar->extract();
        } else {
            die 'No file provided.';
        }
   }

   sub compress {
        my $file_name = $ARGV[0] or die 'No file name specified.';
        if ($file_name !~ /.*\.tar\.gz$/) {
            die 'File name must end with \'.tar.gz\'';
        }
        my $files = $ARGV[1] or die 'No files specified.';
        my $tar = Archive::Tar->new();
        $tar->write($file_name);
        $tar->read($file_name);
        $tar->add_files($files);
        $tar->write($file_name, COMPRESS_GZIP);
   }

Reply via email to