in reply to Re: Parsing a line of text items (updated)
in thread Parsing a line of text items

I can understand the challenge to hack it by yourself ... :)

But I think the suggested Text::ParseWords is core and offers everything I expect from parsing a command line.

It has also tests, is cutomizable and the source is well structured and documented.

So if I "wanna roll my own" and need to make special adjustments (like e.g. paired {quotes} ) I can take the code as a base.

DB<94> use Text::ParseWords qw/shellwords/ DB<96> x shellwords(q{this is 'an example' "with different quoting a +nd \" escaping" including\ escaped\ whitespace}) 0 'this' 1 'is' 2 'an example' 3 'with different quoting and " escaping' 4 'including escaped whitespace' DB<97>

In case larger files need to be parsed I'll consider a dependency to Text::CSV , but this really looks good.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^3: Parsing a line of text items
by AnomalousMonk (Archbishop) on Apr 01, 2021 at 07:01 UTC

    I would tend to agree that an approach using a reliable, common module like Text::ParseWords (of which I had not previously been aware -- thanks, philipbailey++) or Text::CSV is usually best. But I wanted to give an example of a "pure" regex approach.

    As an aside, I think it's worth emphasizing again that whatever approach is taken, a thorough suite of tests for the final code is advisable even if the approach is based on well-tested modules.


    Give a man a fish:  <%-{-{-{-<

      This might sound strange, but these are human interfaces which are hard to test from some level of complexity on.

      I'm sure there are plenty of incompatible solutions, but nobody would notice they are different.

      For instance, what about single quotes without closing partner? Or quotes without neighboring whitespace?

      Do users even expect them to be parsed in a meaningful way?

      The behavior may differ between different solutions, while all are conceived correct.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery