LanceDeeply has asked for the wisdom of the Perl Monks concerning the following question:

i'm having trouble getting Text::ParseWords's parse_line to parse a line with a single quote in it. there was a post on this before, but it doesn't seem resolved. i don't want to have to modify the string being parsed to unescape the single quote.

any ideas?

use Text::ParseWords; my $string = "it can do this"; for ( parse_line ' ', 1, $string ) { print "[$_]\n"; } $string = "but it can't do this"; for ( parse_line ' ', 1, $string ) { print "[$_]\n"; } __DATA__ [it] [can] [do] [this]

Replies are listed 'Best First'.
Re: Text::ParseWords parse_line containing quote
by simonm (Vicar) on Jul 24, 2003 at 18:35 UTC
    Can you clarify what it is you wish to accomplish?

    I assume you're not just looking for space-separated words, for which split(' ', $string) would be sufficient...

    Do you want to ignore single quotes, but treat double-quoted phrases as a single token?

    Without knowing this, it's hard to identify the correct solution...

      I want to use parse_line because of it's nifty parsing of embedded tokens. I am not looking for a split solution. I will be passing different delimiters, and strings into parse_line.

      For this case, I want my output to be:

      [it] [can] [do] [this] [but] [it] [can't] [do] [this]
      instead of just
      [it] [can] [do] [this]

        The quote characters are hard-coded into the operation of parse_line. There's no argument or switch that lets you tell it that you want double-quotes and back-slash escapes to recognized, but to treat single quotes as regular characters.

        It looks like the only way to do what you want is to copy-and-paste the source code for sub parse_line into your script or module, and then go through and remove the ' characters from the key regexes -- for instance, changing "' to ".