# 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; }
In reply to Re: Parsing arguments
by xmath
in thread Parsing arguments
by hv
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |