Postfix dereferencing is stable is v5.24

Perl’s dereferencing syntax might be, or even should be, responsible for people’s disgust at the language. In v5.20, Perl added the experimental postfix dereferencing syntax that made this analogous to method chaining. This is one of the most pleasing Perl features I’ve encountered in years.

Now you can leave out the pragmas to enable the experimental feature and turn off its warning. This example defines a subroutine $gimme_a_ref that returns an array ref that contains a hash ref. In the foreach I dereference the subroutine to get the array ref, use postfix dereferencing to pull out the first array element, then dereference that value to be a hash that I can use with keys. That’s a lot going on, but just like I explained it in this paragraph, I write it from left to right in code:

use v5.24;

my $gimme_a_ref = sub {
	state $hash_ref = {
		cat => 'Buster',
		dog => 'Addy',
		};

	[ $hash_ref ];
	};

foreach my $key ( keys $gimme_a_ref->()->@[0]->%* ) {
	say "key is $key";
	}

my %hash = reverse $gimme_a_ref->()->@[0]->%*;
say Dumper( \%hash ); use Data::Dumper;

If you don’t remove the feature or no warnings or update the version, v5.24 will not issue the experimental feature warnings, or even warnings that this feature is no longer experimental:

# v5.24 won't warn about any of these
use v5.22;
use feature qw(postderef);
no warnings qw(experimental::postderef);

To review the alternatives to this new feature, see the examples I provided in Use postfix dereferencing.

Leave a comment

2 Comments.

  1. I believe the @ in “->@[0]” is superfluous. “->@[…]” gets a slice, and a slice of one will act like you want, here, but the traditional “->[0]” would do.

    That “->%*” though? That’s the stuff!

Leave a Reply


[ Ctrl + Enter ]