in reply to Parsing arguments
# skip initial whitespace, if any $tag =~ /^\s*/gcxs; # look for optional key, and a bareword or the start of a quoted strin +g while ($tag =~ / \G (?: (\w+) \s* = \s*)? ( ['"](?=.) | \w+ ) /gcxs) { my ($k, $v) = ($1, $2); # extract quoted string if ($v eq "'" || $v eq '"') { $tag =~ / \G ( (?: \\. | [^$v] )* ) $v /gcxs or last; $v = $1; $v =~ s/\\(.)/$1/g; # unescape characters } # skip optional separator $tag =~ / \G \s* :? \s* /gcxs; # save value push @args, $k ? [$k, $v] : $v; } # check if parsing was successful die if pos $tag != length $tag;
•Update: If you like, you can ofcourse precompile the quote-patterns so you don't have any variable patterns, like:
and then replace the if-block with:my %quotes; $quotes{$_} = qr/ \G ( (?: \\. | [^$_] )* ) $_ /xs for qw(' ");
But I don't know if that results in any significant increase in speed.if (my $pat = $quotes{$v}) { $tag =~ /$pat/gc or last; $v = $1; $v =~ s/\\(.)/$1/g; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Parsing arguments
by hv (Prior) on Feb 20, 2003 at 19:15 UTC | |
by xmath (Hermit) on Feb 20, 2003 at 19:32 UTC | |
by hv (Prior) on Feb 20, 2003 at 19:55 UTC |