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

+ is the unary-+ operator. It does absolutely nothing but evaluate to the same thing its operand does. It even provides the same context as the one in which it's evaluated.

+5 # Same as `5` +"abc" # Same as `"abc"` +@a # Same as `@a`, in both scalar and list context

${ shift } is just a weird undocumented way of writing $shift. I didn't want that. I wanted to use the shift operator. ${ +shift } can't be parsed as a scalar named +shift, so it has the desired effect.

Replies are listed 'Best First'.
Re^7: Passing argument by reference (for a scalar)
by sleet (Pilgrim) on Sep 11, 2024 at 15:50 UTC

      It's documented to be allowed in string literals, but the OP is not using it in a string literal.

Re^7: Passing argument by reference (for a scalar)
by etj (Priest) on Sep 11, 2024 at 04:36 UTC
    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.
      > 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]
        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.