in reply to Re: Parsing a command-line string into an array
in thread Parsing a command-line string into an array

hmmm, thanks but that opens a different can of worms. I would prefer to rely on a module than string-splitting.

Perhaps I can do something like this way-round-about way:

use Data::Roundtrip qw/json2perl/; use File::Temp; use strict; use warnings; my $cmdlinestr = <<EOC; -x 123 -y 'some val' EOC my ($fh, $fn) = File::Temp::tempfile(); print $fh 'use Data::Roundtrip qw/perl2json/; print perl2json(\@ARGV)' +; close $fh; my $ret = `$^X ${fn} ${cmdlinestr}`; my $args = json2perl($ret); print join("\n", @$args)."\n";

Basically, rely on shell/perl to split the string into @ARGV, by spawning a basic perl script with the cmdline, get a json back and have that back to perl array. A lot can go wrong in this as well.

Replies are listed 'Best First'.
Re^3: Parsing a command-line string into an array
by ikegami (Patriarch) on Dec 23, 2023 at 05:50 UTC

    I would prefer to rely on a module than string-splitting.

    We don't need a module to parse the grammar I suggested. The grammar is just that simple. That's the point.

    All we're doing is parsing the provided value to separate it into a name and a value. And we know the names can't contains spaces or punctuation (except perhaps _ or -).

    This is the same approach curl uses.

    curl ... -H 'Header1: ...' -H 'Header2: ...' ...

    You could leave out the initial dash if you want (-X 'y'/-X 'y ...').

    You could use equal signs instead of spaces if you want (-X 'y'/-X 'y=...').

    Whatever. The specifics aren't important and are quite flexible since all you have to do is separate a name (with a very limited set of characters) from the rest.

    The solution I presented does not "open a different can of worms". It in fact avoids doing so.