Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

ChrisDolan (2855)

  (email not shown publicly)

Journal of ChrisDolan (2855)

Thursday October 30, 2008
12:42 AM

Perl6 surprise: No reference operator

[ #37766 ]

The most surprising change I've discovered so far in Perl6 is that the reference operator ('\') is no more. That is, the following code, while valid in both Perl5 and Perl6, behaves differently in each:

   my %hash;
   my $hashref = \%hash;

In Perl6 the \ operator creates Capture instances instead of references. To make Perl6 behave the way the above code does under Perl5 behavior (where a scalar "contains" the hash), do like this:

  my %hash;
  my $hashref = $(%hash); # itemization

Then to use that $hashref, do like this:

  my @keys = $hashref.keys;
  # Perl5 equivalent:
  # my @keys = keys %{$hashref};

So, the $hashref feels less like a pointer and more like a container. The scalar delegates to its value, which is why the ".keys" works in this example without needing to explicitly dereference the scalar.

The funny thing is that Captures of Lists behave a lot like arrayrefs, so I didn't even notice I had misused the \ in the following code:

  my @array;
  my $arrayref = \@array;
  for @($arrayref) -> $val {
     say $val;

The as-list operator, "@(...)", works about the same on Capture instances as it does on scalars that contain Lists -- in both cases, it returns a list of the enclosed values.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.