Jan Eden wrote: > > Rob Dixon wrote: > > >John W. Krahn wrote: > > >> use File::Find; > >> local ( $^I, @ARGV ) = ''; > >> find( { no_chdir => 1, wanted => sub { -f and push @ARGV, $_ } }, './mydir' ); > >> s/foo/bar/g, print while <>; > > > >Thanks John. > > > >I missed the 'has subdirectories'. > > > > I did not and came up with a similar (but simpler) idea: > > use File::Find; > local $^I = ''; > find( { sub { push @ARGV, $_ if -f }, 'dir' ); > while (<>) { > s/foo/bar/g; > print; > } > > I forgot to empty @ARGV, and I see why it's necessary. But I do not understand John's > anonymous subroutine as find's first argument. Could someone explain it in a little > more detail?
Hi Jan. If you think about it, you'll see that it's an anonymous hash { .. } not an anonymous subroutine sub { .. } In the simple case where you don't need any special options you can pass a code reference as the first parameter to 'find' which is used as the 'wanted' subroutine. John wanted 'no_chdir', so he had to use the full form. This option prevents file() from doing a chdir to the the current directory ($File::Find::dir) before it calls the 'wanted' routine. I would guess two reasons why he did it this way: firstly it should be slightly faster than the default, which is to change directories; secondly it leaves $_ set to the same value as $File::Find::name instead of just the file's basename within the directory so the 'wanted' routine comes out a lot neater. HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>