Exploring Perl 6: Sigil Invariance

| 2 Comments | No TrackBacks

Perl programmers are used to sigils (the funny characters in front of variables) being an indicator of the kind of thing returned by variable expressions. $ means scalar, a single thing, @ means array, an ordered list of things, and % means hash, an associative array. When retrieving a single value from an array or hash the sigil changed to $. When retrieving a slice from a hash, the sigil changed to @.

In Perl 4, all of this made a strange kind of sense. It worked like indicators of quantity in natural languages: the following thing is either singular or plural. In Perl 5, with the addition of scalar references and objects, sigils became inconsistent and confusing. Perl 6 solves this problem by doing away with sigil variance altogether to provide a more consistent and understandable variable syntax.

Let's write a program testing out some of the features of Perl 6's new variable syntax.

#!/usr/bin/env perl6
#file ex2.pl6

my $scalar = "a thing";
my @array  = "a", "list", "of", "things";
my %hash   = larry => 1, curly => 2, moe => 3;

say "a scalar is $scalar";
say "an item from an array is @array[0]";
say "larry is number %hash<larry>";

%hash<shemp> = 4;
@array.push( "another thing" );

say %hash.perl;
say @array.perl;

my @slice = @array[ 2..3 ];
say @slice.perl;
my @nomoe = %hash{ "larry", "curly", "shemp" };
say @nomoe.perl;

What it does

Variable sigils in Perl 6 primarily control the context of assignment expressions and whether structures are flattened in list context. They also provide a rough type constraint system.

Sort Case

  • The $ sigil indicates item assignment and no type constraint. (Technically it is of type Mu, a topic for a later post.)
  • The @ sigil indicates list assignment and a type constraint of Positional. Anything that satisfies the Positional constraint can be assigned to a @-prefixed variable, but Arrays are the default Positional type. @-prefixed variables also flatten in list context.
  • The % sigil indicates list assignment and a type constraint of Associative. Anything that satisfies the Associative constraint can be assigned to a %-prefixed variable, but Hashes are the default Associative type. %-prefixed variables also flatten in list context.
  • Individual hash keys can be accessed with either { } or < > subscripts. The < > form allows the omission of quotes around the hash key.
  • The .perl method can be called on just about anything to dump its contents in a Perl 6-parseable form.

This barely scratches the surface of Perl 6 variables and types. In upcoming posts we will take a look at the Perl 6 type hierarchy and what it can do.

No TrackBacks

TrackBack URL: http://friedo.com/cgi-bin/mt/mt-tb.cgi/29

2 Comments

Mike,

Glad to see your back blogging......

Excellent article.

Leave a comment

About this Entry

This page contains a single entry by Mike Friedman published on January 4, 2016 2:14 AM.

Exploring Perl 6: Up and Running was the previous entry in this blog.

Exploring Perl 6: Numeric Types is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Categories

  • Exploring Perl 6

Pages

Powered by Movable Type 5.14-en