rspishock has asked for the wisdom of the Perl Monks concerning the following question:

I have what's probably a very simple question. I'm finishing a script which pulls software from various locations on a network share and creates a tgz file so that they can be transferred to another system for use. So the question is, can this be done without using Archive::Tar or any other modules? I'm unable to install any modules on this system, thus the problem I'm facing.

I've been searching around google and the monastery trying to find an answer, but everything I've found so far says to use a module.

I have written a similar version of the script in bash, but the Perl version, without including the tar process, finishes in about half the time.

Replies are listed 'Best First'.
Re: Creating a tgz file without a module
by toolic (Bishop) on Sep 08, 2011 at 15:59 UTC
    Archive::Tar has been part of the Core since (effectively) 5.10:
    corelist Archive::Tar Archive::Tar was first released with perl 5.009003

      That's good to know. Unfortunately, I'm currently working with Perl 5.8.8.

Re: Creating a tgz file without a module
by zentara (Cardinal) on Sep 08, 2011 at 16:24 UTC
    I've found passing arguments to tar in a system command can be tedious, but this should work (tested)
    #!/usr/bin/perl use warnings; use strict; my $dir = '.'; system ('tar', '-zcvf', 'my_tarball.tgz', $dir);
    should do the tarr'ing and gunzipping in one step, packing up everything in $dir

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      FYI and FWIW, you can't count on tar recognizing the "-z" option on all flavors of *nix. I've been burned by this before and it depends on the "flavor" of tar installed on the system.


      Peter L. Berghold -- Unix Professional
      Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

        Thanks for the advice. Fortunately, this script will only be running on one system so, at least in this case, everything should be fine.

Re: Creating a tgz file without a module
by blue_cowdawg (Monsignor) on Sep 08, 2011 at 16:05 UTC
        So the question is, can this be done without using Archive::Tar or any other modules? I'm unable to install any modules on this system, thus the problem I'm facing.

    Since you have not specified what environment (WinBloze, *nix, *ix or Macinsquash) I am going to answer with a *nix filter.

    To directly answer your question:

    | hand waving here... system("tar cf tarfile.tar file1 file2 ... filen"); system("gzip -S tgz tarfile.tar");

    But wait! I'm not going to let you off the hook that easily.

    If you are running in a *nix environment you can install modules on your environment by putting them someplace in your HOME directory and then referencing that location with the use lib pragma. WinBloze I can't answer for since I don't do windows.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

      Thanks for the response. I had actually tried to use the

      system "tar zxvfP ....
      line but didn't think it worked since I couldn't find the tgz file which should have been created. Long days on top of lack of sleep tends to have a negative effect on your thinking. Anyway, if nothing else, you've verified that I was originally thinking along the right track.

      For what it's worth, when I said that I can't install a module, it wasn't because of a lack of ability to locate it, it's because of the process I'd have to go through to get it approved to add to my system. Ultimately, it would probably take a long time to get done.

      Thanks again for the guidance.

            ne but didn't think it worked since I couldn't find the tgz file which should have been created

        Try looking for a file ".tar.gz" instead! :-)

            it's because of the process I'd have to go through to get it approved to add to my system. Ultimately, it would probably take a long time to get done.

        Sounds like some form of Change Manglement err.. Management process to me.

        If you have to do this sort of thing on a regular basis (tar up files) I'd say it would be worth the time and trouble get the approvals. I've been there done that and feel your pain.

        In my humble opinion using system is at best a bandaide. I'd rather take the time and trouble to get a job done right in the long run than accept the idea that I can't because of paperwork.


        Peter L. Berghold -- Unix Professional
        Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg