[Tfug] Regexps

Robert Hunter hunter at tfug.org
Thu May 3 02:01:50 MST 2007


> I guess that depends on what you are trying to solve for him.  He
> obviously has a solution that he thinks almost works for him, and he
> is the "man on the ground".

Yes, you are right, Stephen.  I try to avoid stating universal truths,
but once in a while they slip out.  Bad me! ;-)

Sure, you could argue that, depending on what you are doing, using an XML
library for parsing a single value might be overkill, or impractical. 
However, if someone is nice enough to provide me data in an XML format,
you can bet your socks that I will be happy to use an XML library to
parse it.
For folks unfamiliar with XML, and DOM, you will be so glad once you take
the time to learn about them.  The practical applications are awesome.

Here is a Perl code snippet for retrieving George's pressure value from
the weather page.  There are no loops, no regular expressions, hardly any
explicit I/O, and there is even some rudimentary error-checking.

<snip>
my $parser = new XML::DOM::Parser;

my $ua = new LWP::UserAgent;

my $content = $ua->get( $url )->content;

die "Couldn't get $url:$!" unless defined $content;

my $weatherDOM = $parser->parse( $content );

my @pressureNodes = $weatherDOM->getElementsByTagName( 'pressure_in' );

die "I was expecting a single pressure value!" unless scalar @resultNodes
== 1;

print "The pressure today is ",
      $pressureNodes[0]->getFirstChild->toString, "\n";

</snip>

-- 
Rob




More information about the tfug mailing list