Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: uninitialized value $_ in pattern match (m//)

by 7stud (Deacon)
on Jun 23, 2011 at 18:34 UTC ( [id://911126]=note: print w/replies, xml ) Need Help??


in reply to uninitialized value $_ in pattern match (m//)

use strict; use warnings; use 5.010; my $line = '<suite subfamily="Database Connect - SQL" name="DBMgrTest" + family="Databases">'; my $pattern = q{ <suite \s* subfamily="(.*?)" \s* name="(.*?)" \s* family="(.*?)" }; if ( my ($subfamily, $name, $family) = ($line =~ /$pattern/xms) ) { #Successful match, and the left hand side of the equals sign #is a list, and in list context m// returns a list of the #matches for the parenthesized groups say $subfamily; say $name; say $family; } --output:-- Database Connect - SQL DBMgrTest Databases
But that pattern is very brittle. What happens if the attributes are in a different order? Or if there are spaces before or after the = sign? Or if there are single quotes instead of double quotes? HTML and XML are hard to parse, so if there is already a well known module like XML::Simple available, learn how to use that instead.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://911126]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-25 06:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found