📘 Variables in Perl 6: Introspection

Due to the mechanism of introspection, it is easily possible to tell the type of the data living in a variable (a variable in Perl 6 is often referred as a container). To do that, call the predefined WHAT method on a variable. Even if it is a bare scalar, Perl 6 treats it internally as an object; thus, you may call some methods on it.

For scalars, the result depends on the real type of data residing in a variable. Here is an example (parentheses are part of the output):

my $scalar = 42;
my $hello-world = "Hello, World";

say $scalar.WHAT;      # (Int)
say $hello-world.WHAT; # (Str)

For those variables, whose names start with the sigils @ and %, the WHAT method returns the strings (Array) and (Hash).

Try with arrays:

my @list = 10, 20, 30;
my @squares = 0, 1, 4, 9, 16, 25;

say @list.WHAT;    # (Array)
say @squares.WHAT; # (Array)

Now with hashes:

my %hash = 'Language' => 'Perl';
my %capitals = 'France' => 'Paris';

say %hash.WHAT;     # (Hash)
say %capitals.WHAT; # (Hash)

The thing, which is returned after a WHAT call, is a so-called type object. In Perl 6, you should use the === operator to compare these objects.

For instance:

my $value = 42;
say "OK" if $value.WHAT === Int;

There’s an alternative way to check the type of an object residing in a container — the isa method. Call it on an object, passing the type name as an argument, and get the answer:

my $value = 42;
say "OK" if $value.isa(Int);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s