Front page | perl.perl5.porters |
Postings from March 2016
[perl #127640] RFE: "qqw( $var/x $var/y word3 $var4=word4 )"
Thread Previous
|
Thread Next
From:
Brad Gilbert via RT
Date:
March 7, 2016 19:47
Subject:
[perl #127640] RFE: "qqw( $var/x $var/y word3 $var4=word4 )"
Message ID:
rt-4.0.18-18961-1457380033-1447.127640-15-0@perl.org
I'm going to point out that Perl 6 has a vastly different way of parsing strings
that only looks like the Perl 5 way.
On Sat Mar 05 13:29:44 2016, Eirik-Berg.Hanssen@allverden.no wrote:
> On Sat, Mar 5, 2016 at 8:06 PM, Father Chrysostomos via RT <
> perlbug-followup@perl.org> wrote:
>
> > > $x = "good job";
> > > @y = qqw( $x hunter );
> > >
> > > Is @y now ('good', 'job', 'hunter') or ('good job', 'hunter')? I
> > > would think
> > > the latter.
> >
> > I would have assumed the former. So I suppose it’s not obvious. BTW,
> > this does the former:
> >
> > $x = "good job";
> > @y = < $x hunter >;
>
>
> In Perl6, this does the former, too:
>
> eirik@purplehat[22:25:34]~$ perl6 -e 'my $x = "good job"; my @y = « $x
> hunting »; .say for @y'
> good
> job
> hunting
> eirik@purplehat[22:25:42]~$
>
> … which surprised me, until I found that this does the latter:
>
> eirik@purplehat[22:27:52]~$ perl6 -e 'my $x = "good job"; my @y = « "$x"
> hunting »; .say for @y'
> good job
> hunting
> eirik@purplehat[22:27:56]~$
>
> :)
>
>
> Eirik
First off `< ... >` is the same as `qw/ ... /`, and `« ... »` is the same as `qqww/ ... /`
Perl 6 starts off with a zero feature quoting mechanism `Q/ ... /` `「 ... 」` then adds features to it
( That is it doesn't even support backslashing the character used for quoting )
Then there is `'...'` `q/.../` `Q:q/.../` `Q:single/.../` which enables backslashing backslashes and the quoting character
`:qq` / `:double` enables `:c` / `:closure`, `:s` / `:scalar`, `:a` / `:array`, `:h` / `:hash`, `:f` / `:function`
`:w` / `:words` splits on whitespace ( depends on a unicode property )
`:ww` / `:quotewords` splits on whitespace, but takes into consideration quotes
Some combinations can be combined without the colons like `qqww/.../` `qx/.../` `qqx/.../`
( basically one base construct `Q`,`q`,`qq` and one other adverb )
There are several other features that can also be enabled like `:to` for heredocs
Basically `<< ... >>` / `« ... »` is short for
`qqww /.../`
`qq :ww /.../`
`Q :qq :ww /.../`
`Q :double :quotewords /.../`
`Q :closure :scalar :array :hash :function :quotewords /.../`
`Q :closure(True) :scalar(True) :array(True) :hash(True) :function(True) :quotewords(True) /.../`
The spaces between adverbs (feature flags) are optional.
The relevant bit for this discussion:
use v6.c;
my $x = 'Foo Bar';
say qqww/ $x World /.perl; # ("Foo", "Bar", "World")
say qqww/ "$x" World /.perl; # ("Foo Bar", "World")
say qqww/ '$x World' /.perl; # "Foo Bar World"
# :words not :quotewords
say qqw/ '$x World' /.perl; # ("'Foo", "Bar", "World'")
# ignores quotes inside of the embedded string
$x = "'Foo Bar' Baz";
say qqww/ $x World /.perl; # ("'Foo", "Bar'", "Baz", "World")
( Again you could use `<< ... >>` or `« ... »` instead of `qqww/.../` )
Perhaps there should be qw// qqw// qww// qqww//
---
via perlbug: queue: perl5 status: open
https://rt.perl.org/Ticket/Display.html?id=127640
Thread Previous
|
Thread Next