Postfix dereference in Perl
Two spellings of the same deref: sigil-first @$ref and postfix $ref->@*.
use strict;
use warnings;
my $nums = [3, 1, 4, 1, 5];
my $entry = { name => "ada", langs => ["perl", "c"] };
my @classic = @$nums; # sigil-first deref
my @postfix = $nums->@*; # postfix reads left to right
print "classic: @classic\n";
print "postfix: @postfix\n";
print "langs: ", join(", ", $entry->{langs}->@*), "\n";
print "keys: ", join(" ", sort keys $entry->%*), "\n";
print "count: ", scalar $nums->@*, "\n";
How it works
@$numsand$nums->@*produce the same list; postfix reads left to right.- Deep chains stay linear:
$entry->{langs}->@*needs no wrapping braces. $entry->%*derefs a hashref, sokeysandsortapply directly.scalar $nums->@*counts elements, just like the sigil form would.
Keywords and builtins used here
joinkeysmyprintscalarsortuse
The run, in numbers
- Lines
- 14
- Characters to type
- 430
- Tokens
- 110
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 94 seconds.
Step 3 of 3 in Files & modern Perl, step 24 of 27 in Language basics.