http://www.perlmonks.org?node_id=485962


in reply to Quoted Text rule for Parse::RecDescent?

Keep in mind the last item in the production is the one that determines the returned value of the entire production.

Also keep in mind that extract_quotelike behaves different in list context compared to scalar context.

 

The following will do the trick:

{ # Place this block at the top of the grammar, before any rules. use Text::Balanced qw( extract_quotelike ); } string: { [ $item[0], scalar extract_quotelike($text) ] }

Of course, that leaves the quotes in. If you want to remove the quotes, the following will work:

{ # Place this block at the top of the grammar, before any rules. sub dequote_double { local $_ = (@_ ? $_[0] : $_); $_ = substr($_, 1, -1); s/\\(.)/$1/sg; } # Like in Perl. # Unlike bash. sub dequote_single { local $_ = (@_ ? $_[0] : $_); $_ = substr($_, 1, -1); s/\\(['\\])/$1/sg; } } string : /"(?:[^\\"]|\\.)*"/ {[ $item[0], dequote_double($item[1]) ]} | /'(?:[^\\']|\\.)*'/ {[ $item[0], dequote_single($item[1]) ]}

Fortunately and unfortunately, it doesn't parse `, q, qq, s, tr, y and here-docs like extract-quotelike. If you want it to parse any or all of those, reply to this node and I'll provide.

 

By the way, don't use $_ without localizing *_ or at least $_ first. You risk clobbering something in the caller. (for/foreach localize this for you, but not while.)

Update: I pretty much rewrote this node a few times, trying to make my thoughts coherent.