\EXPR evaluates EXPR, and produces a reference to the returned scalar.[1][2]
$ perl -Mv5.14 -e'
my $x = "abc";
say $x;
my $ref = \$x;
say $ref;
'
abc
SCALAR(0x5cd134772030)
$BLOCK evaluates the block in scalar context, and dereferences the scalar returned by the block.
$ perl -Mv5.14 -e'
my $x = "abc";
say $x;
my $ref = \$x;
say $ref;
say ${ $ref };
'
abc
SCALAR(0x5dd7ee254020)
abc
$BLOCK can be used in double-quoted string literals. The value of the dereferenced scalar is interpolated.
$ perl -Mv5.14 -e'
my $x = "abc";
say $x;
my $ref = \$x;
say $ref;
say ${ $ref };
say ">${ $ref }<";
'
abc
SCALAR(0x583cb9035050)
abc
>abc<
Finally, a block can contain multiple statements. The following are equivalent:
my $ref = \$x; ${ $ref }
${ my $ref = \$x; $ref }
${ \$x }
And so are
my $x = PI; my $ref = \$x; ${ $ref }
my $ref = \PI; ${ $ref }
${ my $ref = \PI; $ref }
${ \PI }
- It would be more accurate to use
\LIST, since the operand is always evaluated in list context. If \LIST is evaluated in scalar context (as in our case), \LIST will return a reference to the last scalar produced by LIST.
- Exception:
\VAR returns a reference to the variable.
\( X, Y, ... ) is equivalent to \X, \Y, ....