tags:

views:

57

answers:

3

I have a need to edit cue files in the first directory and not go recursively in the subdirectories.

find(\&read_cue, $dir_source);
sub read_cue {
    /\.cue$/ or return;

    my $fd = $File::Find::dir;
    my $fn = $File::Find::name; 
    tie my @lines, 'Tie::File', $fn
      or die "could not tie file: $!";

    foreach (@lines) {
        s/some substitution//;
    }

    untie @lines;
}

I've tried variations of

$File::Find::prune = 1;
return;  

but with no success. Where should I place and define $File::Find::prune?

Thanks

+4  A: 

If you don't want to recurse, you probably want to use glob:

for  (glob("*.cue")) {
   read_cue($_);
}
msw
I've never used the 'glob' function and have no idea where to put your suggestion in the sub.
thebourneid
_Learning Perl_ might help :)
brian d foy
@thebourneid The code from msw replaces your call to `find()`. Then simply remove the first 3 lines from your `read_cue() method` -- that is, all of the stuff related to `File::Find`, which is the wrong tool for the job in this case.
FM
A: 

Say you have a directory subtree with

/tmp/foo/file.cue
/tmp/foo/bar/file.cue
/tmp/foo/bar/baz/file.cue

Running

#! /usr/bin/perl

use warnings;
use strict;

use File::Find;

sub read_cue {
  if (-f && /\.cue$/) {
    print "found $File::Find::name\n";
  }
}

@ARGV = (".") unless @ARGV;
find \&read_cue => @ARGV;

outputs

found /tmp/foo/file.cue
found /tmp/foo/bar/file.cue
found /tmp/foo/bar/baz/file.cue

But if you remember the directories in which you found cue files

#! /usr/bin/perl

use warnings;
use strict;

use File::Find;

my %seen_cue;
sub read_cue {
  if (-f && /\.cue$/) {
    print "found $File::Find::name\n";
    ++$seen_cue{$File::Find::dir};
  }
  elsif (-d && $seen_cue{$File::Find::dir}) {
    $File::Find::prune = 1;
  }
}

@ARGV = (".") unless @ARGV;
find \&read_cue => @ARGV;

you get only the toplevel cue file:

found /tmp/foo/file.cue

That's because $File::Find::prune emulates the -prune option of find that affects directory processing:

-prune

True; if the file is a directory, do not descend into it.

Greg Bacon
either your question is really unclear, either your answer does not work. OK, you prune subdirectories, but parallel directories will still be searched (just replace your %seen_cue hash with a simple scalar to change that). Also as others pointed out this solution looks overly complex to look in only one directory.
kriss
A: 

If you only want the files in a directory without searching subdirectories, you don't want to use File::Find. A simple glob probably does the trick:

my @files = glob( "$dir_source/*.cue" );

You don't need that subroutine. In general, when you're doing a lot of work for a task that you think should be simple, you're probably doing it wrong. :)

brian d foy