I'm trying to read the content of a file (a.txt) that I just created. This file contains a string consisting of "ABCDE", which I then tar with the write() function. I can see that the "a.tar" is created in my directory, but when I come up to the read() function, I get an error : Can't read a.tar : at testtar.pl line 14.
Am I doing something wrong ? Is it because I am on Windows ?
use strict;
use warnings;
use Archive::Tar;
# $Archive::Tar::DO_NOT_USE_PREFIX = 1;
my $tari = Archive::Tar->new();
$tari->add_files("a.txt");
$tari->write("a.tar");
my $file = "a.tar";
my $tar = Archive::Tar->new() or die "Can't create a tar object : $!";
if(my $error = $tar->read($file)) {
die "Can't read $file : $!";
}
my @files = $tar->get_files();
for my $file_obj(@files) {
my $fh = $file_obj->get_content();
binmode($fh);
my $fileName = $file_obj->full_path();
my $date = $file_obj->mtime();
print $fh;
}
Thanks.
You misunderstand the return value of read
of Archive::Tar
:
$tar->read ( $filename|$handle, [$compressed, {opt => 'val'}] )
Returns the number of files read in scalar context, and a list of
Archive::Tar::File
objects in list context.
Please change the following
if(my $error = $tar->read($file)) {
die "Can't read $file : $!";
}
to
unless ($tar->read($file)) {
die "Can't read $file : $!";
}
and try again.