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

I'm not familiar with ${ +shift }; syntax. It looks like the + is a way to interpret 'shift' as a command inside ${}. I'm not readily finding documentation for this. Where should I look?

Replies are listed 'Best First'.
Re^6: Passing argument by reference (for a scalar)
by choroba (Cardinal) on Sep 10, 2024 at 15:29 UTC
      Thanks!
Re^6: Passing argument by reference (for a scalar)
by LanX (Saint) on Sep 10, 2024 at 21:03 UTC
    > I'm not familiar with

    That's why I prefer

    ${ shift() }

    even if it needs one letter more.

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

Re^6: Passing argument by reference (for a scalar)
by ikegami (Patriarch) on Sep 11, 2024 at 03:13 UTC

    + 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.

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

      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