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


in reply to Parsing a line of text items

I often use Text::ParseWords for this problem. It has the advantage of being a core module.
use strict; use warnings; use feature "say"; use Text::ParseWords; my $args = '23 45.67 "John Marcus" Surname'; my @parsed = parse_line('\s+', 0, $args); say for @parsed;
Output:
23 45.67 John Marcus Surname

Replies are listed 'Best First'.
Re^2: Parsing a line of text items
by mikkoi (Beadle) on Mar 31, 2021 at 11:26 UTC

    Thanks. I had no idea Text-ParseWords existed. This is the ideal solution. And it is in the core!

    I also tested Text-CSV and while good, it left some problems, especially the possible multiple whitespace between words.

      I had no idea Text-ParseWords existed

      Likewise...
      Perhaps more time is needed studying the list of core modules

Re^2: Parsing a line of text items
by LanX (Cardinal) on Mar 30, 2021 at 15:58 UTC
    Oh Text::ParseWords is pretty cool, thanks for sharing. :)

    (So many core modules which need more attention)

    > It has the advantage of being a core module.

    Indeed.

    C:\Strawberry\perl\bin>corelist Text::ParseWords Data for 2021-01-23 Text::ParseWords was first released with perl 5

    Tho it's exporting a lot on default

    our @EXPORT = qw(shellwords quotewords nested_quotewords parse_line);

    And you can tell the documentation is old, could have more examples.

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