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

Hi, I'm using perl5.8, linux. I can't get Archive::Tar to cooperate with File::Find. There are 2 code snippets below. The first works, the second dosn't. Why can't I use the add_files method directly on $File::Find::name? I need to push files into an array, then feed the array to Archive::Tar.
  • Works:
    #!/usr/bin/perl -w use strict; use Archive::Tar; use File::Find; my $tar = Archive::Tar->new(); my $dir = 'somedir'; #some dir with recursive dirs and files my @files; find (\&found,$dir); sub found{ push @files,$File::Find::name; } $tar->add_files(@files); $tar->write('Tmytest2.tgz',9); exit;
    ###########################################
  • While this more direct approach dosn't:
    #!/usr/bin/perl -w use strict; use Archive::Tar; use File::Find; my $tar = Archive::Tar->new(); my $dir = "somedir"; #some dir with recursive dirs and files find (\&found,$dir); sub found{ $tar->add_files($File::Find::name); } $tar->write('Tmytest2.tgz',9); exit;
  • Replies are listed 'Best First'.
    •Re: Using Archive::Tar with File::Find
    by merlyn (Sage) on Jan 07, 2003 at 00:00 UTC
      Without looking at the code, I suspect it's because the found routine is being executed in many different current directories in both cases, but add_files is being handed the wrong path for the job in the second case. When you're in foo/bar, you can't open foo/bar/this.jpg, because it almost certainly doesn't exist. You have to open this.jpg.

      Modern versions of File::Find can be told not to chdir, but that makes them pretty inefficient, so your first program version is probably the best anyway.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

    Re: Using Archive::Tar with File::Find
    by Aristotle (Chancellor) on Jan 07, 2003 at 01:56 UTC
      Just use the excellent File::Find::Rule. :)
      #!/usr/bin/perl -w use strict; use Archive::Tar; use File::Find::Rule; my $tar = Archive::Tar->new(); $tar->add_files(File::Find::Rule->in(@ARGV)); $tar->write('Tmytest2.tgz',9);

      Makeshifts last the longest.