develooper Front page | perl.perl5.porters | Postings from March 2021

Re: Perl feature wish: quote-word array-ref operator

Thread Previous | Thread Next
From:
B. Estrade
Date:
March 13, 2021 04:20
Subject:
Re: Perl feature wish: quote-word array-ref operator
Message ID:
bbef3ef5-e04f-c18f-a222-ef73d1dc025a@cpanel.net


On 3/12/21 9:11 PM, Ricardo Signes wrote:
> On Wed, Mar 10, 2021, at 2:35 AM, Smylers wrote:
>> Eric Wong writes:
>>
>> > One thing that is AFAIK conflict-free could be using a suffix modifier
>> > (similar to regexp modifiers):
>> >
>> > qw[ a b c ]r
>>
>> That's true, but is way harder to read: the gap between the [ and ] can
>> be massive; not knowing what you're assigning until you get to the end
>> of it doesn't make it easy to glance at the code.
> 
> I think this is the most important part of this thread, for me, and for 
> both Eric and Smylers' contributions.
> 
> I like qa[...] just fine, but I it's sort of a one trick pony.  That's 
> okay, not every pony needs to be a polymath, but how many more ponies do 
> we want in this here rodeo, anyway?
> 
> Sticking an "r" at the end lets you have modifiers that express what 
> tricks are being performed… but you don't get to it until the end of 
> reading, so you can't correctly parse the code (as a reader) until you 
> finish reading it and go back.  Or you have to know that "qa" is code 
> for "skip ahead to closing delimiter and then skip back", which is 
> crap.  We're surrounded by the circumfix problem!
> 
> Raku attacked this by putting its modifiers in the beginning.  If we 
> could prepare to do this extensibly, even if only with core-provided 
> extensions, and even with very few to start, we'd be in a good place.
> 
> This example is meant to make the point, not to be exactly what we want:
> 
> qz:d[ ... ]      # :d means "delimiter determines type" so this gets an arrayref
> qz:d{ ... }      #    ...same thing, but we get a hashref
> 
> qz:i{ a $b c }   # :i means interpolative
> 
> qz:x{ this #foo
>        rules }    # :x is like /x on regex; allow comments
> 
> # ...all together now...
> qx:xid[
>    apples
>    bananas
>    cherries
>    $other # <- durians?
>    eggplant
> ]

Looks like the start of "yet another" DSL*. For what it's worth, I use 
the 'my $aref = [qw/foo bar baz/];' syntax quite a bit. qa// seems like 
a nice short cut to this.

What I'd really love to see (or figure out how to do) is this mess when 
order matters:

my $aref = [qw/foo bar baz/, $herp, $derp, @array_o_stuff];

More than once I've DWIM'd variables inside of qw//, only to be reminded 
that this is not a thing.

my $herp = 1;
my $derp = 2;
my @array_o_stuff = (1,2,3);
my $aref = [qw/foo bar baz $herp $derp @array_o_stuff/]; # lolnope wrong

Does quietly something I definitely did not want (though admit this 
could be useful in some cases):

$VAR1 = [
           'foo',
           'bar',
           'baz',
           '$herp',
           '$derp',
           '@array_o_stuff'
         ];

So maybe "qz:i" would be something I'd use. Frankly, I'd rather ditch 
the colon and just have "qwi".

And in my experience, most "new" Perl programmers (letting alone those 
forced to use it) generally pretend the q-operators do not exist; opting 
rather for traditional alternatives to what the offer.

Brett

* E.g., in addition to: REs, XS, pack, printf, format .. I am sure I am 
missing some more.

> 
> 
> -- 
> rjbs

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About