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

Hello monks i wanted to archive all te files in directory. not sure what i am missing. it print the number if files in the directory, but doesn't archive it.
use Archive::Tar; my $dir = 'copy'; my @files = <$dir/*>; my $count = @files; print $count; foreach $file (@files) { print $file; my $tar = Archive::Tar->new; $tar->add_files(@files); }

Replies are listed 'Best First'.
Re: archive using tar
by derby (Abbot) on Apr 20, 2007 at 14:01 UTC

    You need to bring the Archive::Tar constructor out of the loop and after adding to the archive, you need to write the archive.

    my $tar = Archive::Tar->new; foreach $file (@files) { print $file; $tar->add_files(@files); } $tar->write( "foo.tar" );

    Doh! no need for a loop at all

    my $tar = Archive::Tar->new; $tar->add_files( @files ); $tar->write( "foo.tar" );

    -derby