in reply to Re^6: Passing argument by reference (for a scalar)
in thread Passing argument by reference (for a scalar)

A truly useful use of + is for constructing hashes with map:
my %hash = map +($_=>undef), @list;
Without the +, Perl would think you wanted to call map with 2 args, not over @list.

Replies are listed 'Best First'.
Re^8: Passing argument by reference (for a scalar)(Unary Plus)
by LanX (Saint) on Sep 11, 2024 at 10:50 UTC
    > A truly useful use of + is for constructing hashes with map:

    > my %hash = map +($_=>undef), @list;

    For varying definitions of "useful". This needs 2 letters more than the usual notation

    my %hash = map {$_=>undef} @list; °

    update

    °) see choroba's reply why the parser will have trouble interpreting this.

    FWIW: I'd use a hash-slice @hash{@list} = () anyway

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

      Which, in turn, sometimes needs another similar trick:
      my %hash = map { "a$_" => undef } @list; # syntax error at ... line +1, near "} @list" my %hash = map {; "a$_" => undef } @list; # OK.
      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        Granted, in this case you are right. ++

        And I hate this ambiguity, because the parser can't defer parsing the inside of the block till it sees if a comma is following or not

        • } , @list is hash
        • }   @list is block

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

      The "usual notation" you cite has the disadvantage that it has to create, then destroy, a scope for each element of @list, which is an unnecessary performance hit.
        Well those performance hits are often marginal.

        I'd be interested to see a benchmark justifying the disadvantages of higher reading entropy and confused co-maintainers.

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