Hey Shaji,

Thanks for the tip. I'll look into that.


On 05/10/2014 04:23 AM, Shaji Kalidasan wrote:
Dear Mike,

Instead of '-w' you can use 'use warnings' as you are using Perl version 5.10 and above. One more thing for your help subroutine you can use here document instead.

Here is the code snippet

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

I am not going much into the technical aspects. Hope someone will throw more light into the technical details and the best practices.

Shaji

On Sat, May 10, 2014 at 2:31 PM, Mike Dunaway <ekimduna...@gmail.com <mailto:ekimduna...@gmail.com>> wrote:

    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);
        }



--
best,
Shaji
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Your talent is God's gift to you. What you do with it is your gift back to God.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Reply via email to