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

Hi all, I am so-so layman to Perl, and I hit a stumbling block and hoping that this is a quick and easy question. I am write some scripts in Perl on a Windows machine, and I need to handle .tar.gz files. Specifically I need to un-tar.gz them so I can do some regex with them. I am trying to use the Archive::Tar package and I think I am hitting a wall because the example code on the CPAN page is dealing with objects, and I am not an object man. Consider this script:
#!/usr/bin/perl -w use strict; use Archive::Tar; my $tar = Archive::Tar->new; $tar->read('file.tar.gz'); $tar->extract();
So almost an example cut and paste from the CPAN page, now my original thought is that $tar is then a long string that I can do what I want. But when I printed it out I got the following:
Archive::Tar=HASH(0x2dd2a4)
Ok... So it is a hash, but I don't know what the keys are, so doing a little digging in Perl Cookbook I get a snippet of code to tell me what the keys are and find out that $tar is not a hash... stumped... In theory all I would like to do it dump the contents of the tar.gz file into a string then slice and dice, but I can't seem to get to the contents. Can any one point me in the right direction? I would really appreciate it. Cheers

Replies are listed 'Best First'.
Re: Archive::Tar basic question
by graff (Chancellor) on Aug 04, 2009 at 03:12 UTC
    It sounds like you haven't quite grokked the overall mindset of Archive::Tar, which uses objects rather than simple perl data types like strings and hashes.

    So, you start with the "tar container" object (the thing returned by Archive::Tar::new), and use its methods to do things -- e.g., open a tar file and fetch the various "tar content" objects (directories and files), and then use the methods of those objects to see their various attributes (data content of files, dates, owners, permissions, etc).

    In case it helps, I posted a review of this module a while back. If you have any questions after reading that, show us some more detail of what you hope to accomplish, what you've actually tried, and how that's falling short. (I'm hoping that my review hasn't become terribly outdated due to module updates...)

      Hi Graff,
      Thank you for providing the link, the tutorial you wrote is very helpful, and it looks like I can move forward. Thanks again.
      Cheers.
Re: Archive::Tar basic question
by halfcountplus (Hermit) on Aug 04, 2009 at 01:29 UTC
    $tar is an object. Generally, perl objects happen like this:
    sub new { my $self = {}; [...] bless($self); }
    $self is declared as a hash reference, and returned as a "bless'd" reference -- that is, an object. Qv.

    http://www.perl.com/doc/manual/html/pod/perlobj.html

    So you have to use "object", aka. "indirect", notation with it:
    $tar->{key};
    This is distinct from, but similar to, the syntax for calling a method, which is a function in the module.
    $tar->method();
      How would I list out the keys to use here? Thank you for taking the time to explain.