2

I've just come across such usage of Perl constants:

use strict;
use warnings;
use constant PI => 4 * atan2(1, 1);

print("The value of PI is: ${\PI}\n");  # The value of PI is: 3.14159265358979

How does this syntax for interpolating a constant into a string work?

2 Answers 2

4

\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 }

  1. 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.
  2. Exception:
    • \VAR returns a reference to the variable.
    • \( X, Y, ... ) is equivalent to \X, \Y, ....
Sign up to request clarification or add additional context in comments.

6 Comments

Almost. \ gives list context and generates a reference to each value in the list, even though ${} gives scalar context so discards all but the last reference
@ysth, Not quite either. It's \ that discards the excess. perl -Mv5.14 -e'say for scalar \sub { say wantarray ? 1 : 0; qw( a b c ) }->()' produces but one ref. Fixed.
Right, I meant deref gives scalar context to \ so that discards all but the last. And actually not sure if it does generate all the references or just one. Cute demo: "is dst ${\localtime}"
FWIW \$arrayref->@* also gets a reference to the array, not a list of references to its elements. I wasn't able to do the same with an lvalue sub that returned an array.
@ysth yeah, I assumed as much, and I considered it covered by \VAR. The docs don't actually use VAR anywhere, so it's unclear what it covers. Intentionally. That way, it can cover var-like constructs like the one you presented if applicable.
Just actually read the doc, it says "sigilled thing" :). Also points out (a little obliquely) that \(...) also takes references to each thing in the not flattened list
1

This declares the constant PI at compile-time:

use constant PI => 4 * atan2(1, 1);

When the constant is used in an expression, Perl replaces it with its value at compile time. So the print (on my machine) becomes equivalent to:

print("The value of PI is: ${\3.14159265358979}\n");

Now, ${\3.14159265358979} is just taking a reference to a scalar value with the \ operator and then dereferencing it using the ${..} block construct.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.