On 9/11/07, kapil.V <[EMAIL PROTECTED]> wrote: > Hi, > > #!/usr/bin/perl -w > my $path = shift; > my $machine = shift; > my (undef,$country, $property) = split /\//,$path; > my $xmlData = qx!./pindown. php $machine!; > my @contacts = $xmlData =~ /property name=\"$property\ " > country=\"$country\ ">.+?<contact type=\"admin\ ">.+?<\/contact> /is; > #print "$&\n"; > print @contacts; > > This prints "1" > But printing $& prints a block of XML. > Why does the array contacts not contain the pattern matched?(Also tried > assigning the matched pattern to a scalar.) snip
Because you have no captures. In list context the m// operator returns a list of captures, not matches. Since you have no captures, the m// operator is falling back to returning true because the pattern matched. from perldoc perlop If the "/g" option is not used, "m//" in list context returns a list consisting of the subexpressions matched by the parenthe‐ ses in the pattern, i.e., ($1, $2, $3...). (Note that here $1 etc. are also set, and that this differs from Perl 4's behav‐ ior.) When there are no parentheses in the pattern, the return value is the list "(1)" for success. With or without parenthe‐ ses, an empty list is returned upon failure. Please also note that it is an incredibly bad idea to try and parse XML with regular expressions. Use XML::Twig or some other generic XML parser. For instance, the XML you are trying to parse only needs one change to break your regex: "contact type =". That one, perfectly valid in XML, space breaks your regex; however, a proper XML parser would not be fazed by it. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/