Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Indirect Object Syntax

by NERDVANA (Pilgrim)
on Nov 07, 2021 at 09:07 UTC ( #11138542=note: print w/replies, xml ) Need Help??


in reply to Indirect Object Syntax

and because Perl's interpreter could potentially get this wrong, so could any human trying to understand the code.

I think it is less about people understanding the intent of the author, and more about having newer Perl users get frustrated when a syntax that worked in one part of the code doesn't work in a different part of the code. Perl has several of these problems, and we can eliminate one of them by giving up on the indirect method syntax.

An example of another ambiguity is when you want to generate multiple elements per iteration of "map".

%set= map { $_ => foo($_) } @list; %set= map +( $_ => foo($_) ), @list;

The first syntax is more idiomatic and looks better, but sometimes (depending on the code in the block and maybe also what comes after it) perl will decide that the block was a hashref notation and give you a nonsense error message. I've started always using the second notation just because it never parses incorrectly. This is *not* a universal recommendation, just a personal preference.

Replies are listed 'Best First'.
Re^2: Indirect Object Syntax
by LanX (Sage) on Nov 07, 2021 at 09:16 UTC
    > I've started always using the second notation just because it never parses incorrectly.

    I'd rather go for extra semicolon:

    • %set= map {; $_ => foo($_) } @list;
    demo with perl -de0
    DB<45> x map {a=>1} 1..3; # seeing hash, but co +mma missing syntax error at (eval 55)[c:/Strawberry/perl/lib/perl5db.pl:738] line +2, near "} 1" DB<46> x map {a=>1}, 1..3; # seeing hash again 0 HASH(0x2f501e8) 'a' => 1 1 HASH(0x2f4a1e8) 'a' => 1 2 HASH(0x2f4d0e0) 'a' => 1 DB<47> x map {;a=>1} 1..3; # seeing code 0 'a' 1 1 2 'a' 3 1 4 'a' 5 1 DB<48>

    YMMV! :)

    edit
    Anyway, Perl should check for the missing comma. I suppose this happens too late for the parser.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      > Anyway, Perl should check for the missing comma. I suppose this happens too late for the parser.

      IIRC, the parser can only check the following token when deciding whether the { starts a block or an anonymous hash. That's also why the ; helps.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        yeah, that's what I meant with

        > I suppose this° happens too late for the parser.

        °) i.e. the comma

        edit

        Anyway: any correctly parsed LIST inside an {anonymous hash} is also a legal block (just the LIST returned)

        Perl could always try to parse a block and look if there is a comma following.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11138542]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2022-05-26 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (91 votes). Check out past polls.

    Notices?