0

in blah.txt:

/a/b/c-test

in blah.pl

  1 my @dirs;
  2 $ws = '/a/b/c-test/blah/blah';       <--- trying to match this
  3 sub blah{
  4     my $err;
  5     open(my $fh, "<", "blah.txt") or $err = "catn do it\n";
  6     if ($err) {
  7         print $err;
  8         return;
  9     } else {
 10         while(<$fh>){
 11             chomp;
 12             push @dirs, $_;
 13         }
 14     }
 15     close $fh;
 16     print "successful\n";
 17 }
 18
 19
 20 blah();
 21
 22 foreach (@dirs) {
 23     print "$_\n"; #/a/b/c-test
 24     if ($_ =~ /$ws/ ) {                  <--- didnt match it
 25         print "GOT IT!\n";
 26     } else {
 27         print "didnt get it\n";
 28     }
 29 }
~

perl blah.pl

successful
/a/b/c-test
didnt get it

I am not quite sure why it is not matching. Anyone know?

ealeon
  • 12,074
  • 24
  • 92
  • 173

1 Answers1

3

Consider,

if ($ws =~ /$_/ ) {  

instead of,

if ($_ =~ /$ws/ ) {  

as /a/b/c-test/blah/blah contains /a/b/c-test string, not otherwise.

As a side notes:

  • use at least strict and warnings
  • read and process file in while() loop instead of filling array first
  • if you must fill array, use my @dirs = <$fh>; chomp(@dirs);
Community
  • 1
  • 1
mpapec
  • 50,217
  • 8
  • 67
  • 127
  • i was hoping to save it in the array first because this will be called multiple times and i dont want to keep opening the file – ealeon Jan 30 '14 at 19:39