in reply to No xml module please

Parsing XML is best done with the aid of one of the XML modules, such as XML::Simple.

But if you are allergic to modules and your string always has the fixed format shown above, you can use quick and dirty shortcuts that will bite you when the format cahnges :)

A hack such as this will do the trick:

my $string = "<message><file>D:\linkctltr.cxx</file><line>68</line><ty +pe>Note</type><codee>970</codee><desc>Use outside of a typedef</desc> +</message>"; $string =~ /<file>([^<]+)/; my $file = $1; $string =~ /<line>([^<]+)/; my $line = $1; $string =~ /<type>([^<]+)/; my $type = $1; $string =~ /<codee>([^<]+)/; my $codee = $1; $string =~ /<desc>([^<]+)/; my $desc = $1;
-Mark

Replies are listed 'Best First'.
Re: Re: No xml module please
by Ovid (Cardinal) on Jul 18, 2002 at 16:04 UTC

    kvale: if you must introduce this person to the evil of regexes ;), at least watch out for a subtle bug that you've introduced. What happens if your first match succeeds and the rest fail? The subsequent variables will be set to the first one's value, thus hampering error checking. I would rewrite that as follows:

    my ($file) = $string =~ /<file>([^<]+)/; my ($line) = $string =~ /<line>([^<]+)/; my ($type) = $string =~ /<type>([^<]+)/; my ($codee) = $string =~ /<codee>([^<]+)/; my ($desc) = $string =~ /<desc>([^<]+)/;

    That should be a bit safer. Or better yet, go with a hash:

    my %info; # a better name should be picked foreach ( qw/ file line type codee desc / ) { my ($info{$_}) = $string =~ /<\Q$_\E>([^<]+)/; }

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Good point. My code will work for the XML as given, but there is no good reason to make brittle code yet more brittle. Thanks for the improvements.

      -Mark