Ask Bjoern Hansen writes ..

>On Mon, 30 Apr 2001, Meije Oppenhuizen wrote:
>
>> I am probably doing something very wrong here, but can 
>someone tell me
>> why
>> 
>> #!/usr/bin/perl -w
>> 
>> use File::Find;
>> 
>> print "$arg";
>> open(LISTFILE, "> /home/meyeo/testfile") or die "Can't open the ffin
>> thingy!";
>> print LISTFILE "\n";
>> close(LISTFILE);
>> 
>> find (\&wanted, $arg);
>> 
>> sub wanted
>> {
>> #  this should print all the files in the serving directory 
>(including
>> subdirs)!
>> open(LISTFILE, ">> /home/meyeo/testfile") or die "Can't open the ffin
>> thingy!";
>> print LISTFILE "$_\n";
>> close(LISTFILE);
>> }
>
>I can't see why it works differently in different places, but
>something like this would be MUCH more efficient:
>
>#!/usr/bin/perl -w
>use strict;
>use File::Find;
>
>my $arg = shift @ARGV;
>
>my @files;
>
>find (sub { push @files, $_ }, $arg);
>
>open LISTFILE, ">>outputfile" or die "Could not open
>outputfile: $!";
>print LISTFILE join "\n", @files;
>close LISTFILE;
>
>.... of course, that will have to keep a list in memory of all the
>files it has read so far. If that's a problem, then you can change
>it so it will call a "flush_list" function every 5000 file or
>something and clear the @files array.


or you could just open the filehandle before the call to find - then print
to it within the find .. eg.

  #!/usr/bin/perl -w
  use strict;

  use File::Find 'find';

  my $arg = shift;        # shifts @ARGV by default

  open LISTFILE, '>>outputfile' or die "Bad open: $!";

  find( sub { print LISTFILE "$_\n" }, $arg);

  close LISTFILE or die "Bad close: $!";  # check closes on write handles

  __END__


NB: I didn't test the above code .. so excuse any typo syntax errors

-- 
  jason king

  In Norway, you may not spay your female dog or cat.  However, you may
  neuter the males of the species. - http://dumblaws.com/

Reply via email to