Is there a bash/shlex-like processor with double-quotes handling?

Is there a function that can decompose a string to an array separated by 
whitespace, but also respecting double quotes, and prefereably escape 
sequences?

So, for example:

my $d="hello   \"cruel world\"";

something-something($d) ; => ("hello", "cruel world")
0
alt
7/14/2017 7:41:22 AM
perl.perl6.users 834 articles. 0 followers. Follow

3 Replies
27 Views

Similar Articles

[PageSpeed] 16

--f403045c213cb393000554437335
Content-Type: text/plain; charset="UTF-8"

If you haven't yet, you might want to look into a CSV parser. I think that
if you configure one of those to split on whitespace, that should give you
the results you want.

(Now with added reply all.)

On Fri, 14 Jul 2017, 08:42 Mark Carter, <alt.mcarter@gmail.com> wrote:

> Is there a function that can decompose a string to an array separated by
> whitespace, but also respecting double quotes, and prefereably escape
> sequences?
>
> So, for example:
>
> my $d="hello   \"cruel world\"";
>
> something-something($d) ; => ("hello", "cruel world")
>

--f403045c213cb393000554437335
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div>If you haven&#39;t yet, you might want to look into a CSV parser. I th=
ink that if you configure one of those to split on whitespace, that should =
give you the results you want.<br></div><div><br></div>(Now with added repl=
y all.)<br><br><div class=3D"gmail_quote"><div dir=3D"ltr">On Fri, 14 Jul 2=
017, 08:42 Mark Carter, &lt;<a href=3D"mailto:alt.mcarter@gmail.com">alt.mc=
arter@gmail.com</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" s=
tyle=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Is t=
here a function that can decompose a string to an array separated by<br>
whitespace, but also respecting double quotes, and prefereably escape<br>
sequences?<br>
<br>
So, for example:<br>
<br>
my $d=3D&quot;hello=C2=A0 =C2=A0\&quot;cruel world\&quot;&quot;;<br>
<br>
something-something($d) ; =3D&gt; (&quot;hello&quot;, &quot;cruel world&quo=
t;)<br>
</blockquote></div>

--f403045c213cb393000554437335--
0
philip
7/14/2017 9:12:33 AM
zef install Text::CSV

This is a native port of Perl 5=E2=80=99s Text::CSV by the original =
author.

> On 14 Jul 2017, at 11:12, Philip Hazelden <philip.hazelden@gmail.com> =
wrote:
>=20
> If you haven't yet, you might want to look into a CSV parser. I think =
that if you configure one of those to split on whitespace, that should =
give you the results you want.
>=20
> (Now with added reply all.)
>=20
> On Fri, 14 Jul 2017, 08:42 Mark Carter, <alt.mcarter@gmail.com> wrote:
> Is there a function that can decompose a string to an array separated =
by
> whitespace, but also respecting double quotes, and prefereably escape
> sequences?
>=20
> So, for example:
>=20
> my $d=3D"hello   \"cruel world\"";
>=20
> something-something($d) ; =3D> ("hello", "cruel world")
0
liz
7/14/2017 10:37:42 AM

On 14/07/2017 15:23, Lucas Buchala wrote:
> Alternatively, the «...» builtin operator already does some kind of
> word splitting respecting quotes, if that fits your needs:
>

Thanks. I take it you are referring to the "hyper" operator (?) It looks 
good, but it doesn't quite work:
  my $d = "hello \"there world\"";
my @f = << $d >>;
  @f[1] ; # "there


As it happens, I did manage to create a grammar that respects quoting, 
and even escaped quoting:

grammar Shlex {
         token TOP {<.ws>? (<word>|<qstring>)* <.ws>?}
         token ws { \s+ }
         rule word { <[a..zA..Z0..9\\-]>+ }
         token ascii_char { <-["\\]> } # anything not a " or \
         token escaped_char { "\\\"" } # literal \"
         token qstring { '"' [<escaped_char>|<ascii_char>]* '"' <.ws>?  }
}

my $m = Shlex.parse(Q[goodbye "\"cruel\" world"  ]);
say "First  component:$m[0][0].Str()"; # OUTPUT: First  component:goodbye
say "Second component:$m[0][1].Str()"; # OUTPUT: Second component:"\"cruel\" world"
0
alt
7/14/2017 3:17:31 PM
Reply: