in reply to 2 Questions on File::Find
Anonymous subs are created using sub { }.
Just declare the variable outside of find.
use File::Find; my $dir; find( sub { $dir = $File::Find::name if ( /hello\.txt/ ); }, '/tmp/test' );
Unfortunately, find will keep on searching for more matches. You can exit prematurely using die
use File::Find; my $dir; if (!eval { find( sub { die "match: $File::Find::name\n" if ( /hello\.txt/ ); }, '/tmp/test' ); 1 }) { my $e = $@; die $e if !( ($dir) = /^match: (.*)/ ); }
|
|---|