Mumia W. am Montag, 12. Februar 2007 21:53:
> On 02/12/2007 02:33 PM, Vladimir Lemberg wrote:
> > Hi,
> >
> > I have a script, which suppose to find all *.xml files under the
> > specified directory then process them. I'm facing the pattern match
> > problem:
> >
> > use strict;
> > use warnings;
> > use Win32;
> > use File::Find;
> >
> > @ARGV = Win32::GetCwd() unless @ARGV;
> >
> >
> > my @dirs;
> >
> > find (\&FindXml, $ARGV[0]);
> >
> > sub FindXml
> > {
> >     return if !stat || -d;
> >     ( my $xml_file = $File::Find::name ) =~ /^.+\.xml$/;
> >     push ( @dirs, $xml_file );
> > }
> >
> > In this examples the pattern match /^.+\.xml$/ is not working and all
> > files regardless of the extension have been assigned to $xml_file
> > variable. [...]
>
> You probably want to push the filename onto the array if you get a
> successful match:
>
>      sub FindXml
>      {
>          return if !stat || -d;
>          ( my $xml_file = $File::Find::name ) =~ /^(.+\.xml)$/;
>          push ( @dirs, $xml_file ) if $1;
>      }
[snipped]

Mumia,

This will not behave as expected because a test is missing if the match was 
successful. I mention it mainly because it's a common error that is not easy 
to detect at first glance :-)

Test script demonstrating the issue:

#!/usr/bin/perl

use strict;
use warnings;

my $pat='a';

for (qw/ a b c d/) {
  (my $x=$_)=~/(^$pat$)/;
  print "Found $pat in /$_\n" if $1;
}

__END__

# Output is:
Found a in a
Found a in b
Found a in c
Found a in d


Dani

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to