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

I'm having some trouble understanding how to retrieve [\@properties] of files inside of Tarballs. This information will be later saved into a table.

Here is the information from the perldoc.

Archive::Tar->list_archive($file, $compressed, [\@properties])

Returns a list of the names of all the files in the archive. The first argument can either be the name of the tar file to list or a reference to an open file handle (e.g. a GLOB reference).

If list_archive() is passed an array reference as its third argument it returns a list of hash references containing the requested properties of each file. The following list of properties is supported: full_path, name, size, mtime (last modified date), mode, uid, gid, linkname, uname, gname, devmajor, devminor, prefix, type.

I've tried a few different test code samples but none are giving me the properties and I haven't seen any code samples online showing someone else trying to get properties.

use Archive::Tar; my @properties = "name, size, mtime"; my ($location) = "/vol/archive5/test5.tar"; my %files = Archive::Tar->list_archive($location,,[\@properties]); print Dumper \%files;
use Archive::Tar; my ($location) = "/vol/archive5/test5.tar"; my %files = Archive::Tar->list_archive($location,,['size']); foreach my $file (keys %files) { my $size = $files{$file}{'size'}; print "$size\n"; }
use Archive::Tar; my ($location) = "/vol/archive5/test5.tar"; my %files = Archive::Tar->list_archive($location,,['size']); foreach my $file (keys %files) { my $size = @{ $files{$file}{'size'} }; print "$size\n"; }

Replies are listed 'Best First'.
Re: Archive::Tar->list_archive Properties
by hdb (Monsignor) on Mar 10, 2015 at 13:33 UTC

    Reading the docs I would try

    use Archive::Tar; my @properties = qw( name size mtime ); my $location = "/vol/archive5/test5.tar"; my %files = Archive::Tar->list_archive($location,0,\@properties); print Dumper \%files;

    UPDATE: my posted code seems to work...

    UPDATE: the output looks funny though:

    $VAR1 = { 'HASH(0x3148770)' => { 'mtime' => 1351116756, 'name' => 'SuffixTree-0.07/t/test_str +ing.t', 'size' => 742 }, 'HASH(0x2f9e870)' => { 'mtime' => 1351116816, 'name' => 'SuffixTree-0.07/suffix_tre +e.h', 'size' => 6075 }, 'HASH(0x3148410)' => { 'mtime' => 1351115894, 'name' => 'SuffixTree-0.07/t/13-kwali +tee.t', 'size' => 691 }, ...

    Design-wise, an array of hash references would make more sense imho.

Re: Archive::Tar->list_archive Properties
by toolic (Bishop) on Mar 10, 2015 at 13:45 UTC
    use warnings; use strict; use Archive::Tar; use Data::Dumper; $Data::Dumper::Sortkeys=1; my @properties = qw(name size mtime); my $location = "xxx.tar"; my @files = Archive::Tar->list_archive($location, 0, \@properties); print Dumper(\@files);

    Several issues:

    • Your @properties array has only 1 element, not 3.
    • [\@properties]: The squares brackets in the docs mean that the array ref is optional. You should omit the brackets in your code.
    • See Corion's explanation of the 2nd arg: $compressed
    • list_archive seems to return a list of hash refs, rather than a hash, which becomes more apparent when you use warnings.

      Thanks for your comment on the return type, this makes much more sense!

      Thank you so much. I knew I must be close to the answer. This helped explain it.
Re: Archive::Tar->list_archive Properties
by Corion (Patriarch) on Mar 10, 2015 at 13:36 UTC

    Note that Perl is not Visual Basic. ,, does not create an empty element:

    use strict; use Data::Dumper; sub print_args { print Dumper \@_; }; print "Plain:\n"; print_args( 'foo', 'bar' ); print "With ',,':\n"; print_args( 'foo',, 'bar' ); print "Explicit:\n"; print_args( 'foo', undef, 'bar' );