use File::Find;
foreach my $dir (@INC){
find sub {
$searchstr= 'C:/Perl/lib/Time/Local.pm';
if( $searchstr =~ "$dir/$_" ){
print " Module $File::Find::name is found\n" ;
$localflag=1;
}
},$dir;
}
when $dir/$_ becomes "C:/Perl/lib/Time/Local.pm" the condition is not becoming true. monks please advice on the same.
2006-08-03 [id://340870|Retitled] by [planetscape], as per Monastery [id://341118|guidelines]
Original title: 'matching with special charcters'
Hi [madtoperl], Try this,
use File::Find;
foreach my $dir (@INC){
find (sub {
$searchstr= 'C:/Perl/lib/Time/Local.pm';
if( $searchstr =~ "\Q$File::Find::name\E" ){
print " Module $File::Find::name is found\n" ;
$localflag=1;
}
},$dir);
}
__END__
Module C:/Perl/lib is found
Module C:/Perl/lib/Time is found
Module C:/Perl/lib/Time/Local.pm is found
Or Change if statement as:
if( $searchstr eq "$File::Find::name" ) Output is:(Alone) ----------------- Module C:/Perl/lib/Time/Local.pm is found
Regards,
Velusamy R.
Try running that with use warnings; and read what it it tells you.
Update: Grr! I think I need glasses - I can't tell the difference between = and ~ this morning.
/J\
use strict;
use warnings;
use File::Find;
no warnings File::Find; # see documentation on File::Find
my $searchstr= 'C:/Perl/lib/Time/Local.pm';
my $found = 0;
find sub {
$found = ( $File::Find::name =~ /$searchstr/ )
and goto DONE01; # quit whole @INC search when found
}, @INC;
DONE01:
# ...
Update: The 'goto' is only necessary using a module - so there is a strong temptation to use a hand-rolled class rather than the non-object-oriented FILE::Find, perhaps not just to write a 'quit' method, but more to gain better control of the functionality, given that such a class can be done in half a page e.g.
use strict;
use warnings;
package MyFind;
our $name;
sub new {
my $self = shift;
$self -> { SUBREF } = shift; # ref to sub to be
# called per file found
my @dirs = @_;
$self -> { DIRS } = \@dirs;
$self -> { QUIT } = 0;
}
sub find {
my $self = shift;
my $aref = $self -> { DIRS };
for my $dir ( @$aref ) {
traverse( $self, $dir );
last if $self -> { QUIT };
}
}
sub quit {
my $self = shift;
$self -> { QUIT } = 1;
}
sub traverse {
my $self = shift;
my $dir = shift;
opendir my ($dh), $dir;
for my $file ( grep !/^\./, readdir $dh ) {
$name = "$dir/$file";
last if ( $self -> { QUIT } );
traverse( $self, $name ) if ( -d $name );
$self -> { QUIT } or &{$self -> { SUBREF }};
}
closedir $dh;
}
1;
-M
Free your mind
perlmonks.org content © perlmonks.org and gellyfish, madtoperl, Moron, Samy_rio
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03