matching with special characters
madtoperl
created: 2006-08-02 02:17:18
Dear monks,

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] ( keep:1 edit:21 reap:0 )
Original title: 'matching with special charcters'

Re: matching with special characters
created: 2006-08-02 02:34:34

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.


eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: matching with special characters
created: 2006-08-02 03:41:17

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\

Re: matching with special characters
created: 2006-08-02 07:55:06
Instead of looping @INC it is much easier to hand that to 'find'. Adjusting also for performance (no need to let find continue when you're done) and for certain quality measures generally accepted at this site, I would end up with something like this:
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