in reply to separated quoted stings from unquoted strings
The second argument to parse_line tells it to retain the quotes it found. This function returns nothing in the case of unbalanced quotes, so we can use this to detect an error. Also note the order of the two greps. The substitute in the second one operates directly on the elements of @words, not on the intermediate $_ (which I have just learned while concocting this example). So it cannot come before the one above it.use strict; use Text::ParseWords; my $string = 'one "two and a half" "three" four "five" "six"'; my @words = &parse_line('\s+', 1, $string); my $error = length($string) && @words == 0; my @unquoted = grep {!m/"/} @words; my @quoted = grep {s/^"(.*)"$/$1/} @words; print join('|', @unquoted)."\n"; print join('|', @quoted)."\n"; print $error;
|
|---|