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

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

Thread Previous | Thread Next
From:
Dan Book
Date:
March 12, 2021 16:06
Subject:
Re: Perl feature wish: quote-word array-ref operator
Message ID:
CABMkAVXh+9rOBxZrBd9RuteGb6wUzpCTF8VjG7tEqgvUb7arUg@mail.gmail.com
On Fri, Mar 12, 2021 at 5:27 AM Salvador FandiƱo <sfandino@gmail.com> wrote:

> On 12/3/21 10:35, H.Merijn Brand wrote:
> > On Thu, 11 Mar 2021 12:43:42 -0500, Dan Book <grinnz@gmail.com> wrote:
> >
> >> On Thu, Mar 11, 2021 at 3:30 AM Alexander Hartmaier <
> >> alex.hartmaier@gmail.com> 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.
> >>
> >> Just FYI this is because Perl identifiers don't consider hyphens to
> >> be word characters (a decision that Raku, as mentioned, has done
> >> differently). This applies to all types of identifiers such as
> >> variable and package names, bareword hash keys are just one place
> >> where identifier rules are used:
> >> https://perldoc.perl.org/perldata#Identifier-parsing
> >
> > But the fat comma already makes an exception for leading dashes:
> >
> > $ perl -MData::Dumper -E'say Dumper ({ -key => 42, -value => "Boo" })'
> > $VAR1 = {
> >            '-key' => 42,
> >            '-value' => 'Boo'
> >          };
>
> That's exactly what I though yesterday after reading Alexander mail, but
> then I got to the documentation (didn't have the time to go into the
> source) and found that the magic is not attached to the fat comma but to
> the unary minus operator!
>
> See https://perldoc.perl.org/perlop#Symbolic-Unary-Operators
>
> I suppose this is the mechanism also making "$foo{-bar}" work.
>

This is correct. `my $str = -foo;` and `use Foo -bar;` are perfectly legal
and strict-safe because of the unary minus.

-Dan

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