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

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

Thread Previous | Thread Next
March 11, 2021 10:13
Re: Perl feature wish: quote-word array-ref operator
Message ID:
On 11.03.2021 09:29, Alexander Hartmaier wrote:
> What I hate about quoting in Perl in general is that it doesn't allow to use the dash! my 
> %hash = ( foo-bar => 'baz' ); doesn't work and needs to be quoted.

The fact that
%hash = ( foobar => 'baz' );
works, and automatically quotes the key (*), is "perl syntactic sugar":  it's nice because 
it saves some tedious typing sometimes, and it is in the (historical) spirit of perl (**).
(And perl programmers being the feisty guys they are, it's probably used in thousands of 
programs and modules by now).

But in my opinion, it is also one of these things which make the language nowadays get 
this reputation for being rather "less rigorous" than others (***).  In all other 
languages that I know of, you would *always* need to quote literal keys and values, when 
you fill a hash :

%hash = ( 'foobar' => 'baz' );

So maybe we should just lay back and enjoy the time we save in typing every time it works, 
and just remember that sometimes it doesn't and then we have to do it the hard way. And to 
re-use the time saved, we can just have a beer or a coffee.

(*) The fact that it doesn't work when the key is foo-bar, is probably due to the 
difficulty for the interpreter to distinguish this form from other things. I imagine that 
when it finds the "=>", it needs to back-track to quote the previous token. But hey, when 
it finds the "-", what should it do ?
And to correct that, you probably have to fix so many other things, that you end up with 
Raku instead of perl.

(**) "There is more than one way to do it", "laziness, hubris, etc..". For many things, 
perl allows you to be rather "sloppy" and get away with it.
But sometimes it bites..

(***) I mean, in the minds of purists and programming language theoricians.
For perl enthousiast users like me of course, it is just plain fun.

Personally, I try to always quote the keys, even when I don't have to.
But I'm having the beer anyway.
Because I figure that I save all the times in the future when I would otherwise have to 
explain to a colleague (or to myself) what I was trying to do there.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About