Course of Raku / Advanced / Containers / Introspecting containers

WHO and HOW

Two more introspection tools complete the set: HOW and WHO.

HOW

Every value in Raku is backed by a metaobject — an object that knows how the value’s type works. HOW (short for Higher Order Workings) returns that metaobject:

my $x = 42;
say $x.HOW.^name; # Perl6::Metamodel::ClassHOW

You have been using the metaobject all along, perhaps without noticing. The .^ in .^name is a method call routed through HOW. These two lines are equivalent:

my $x = 42;
say $x.^name;        # Int
say $x.HOW.name($x); # Int

So $x.^name is just a shorter way of writing $x.HOW.name($x). Notice that the object is passed in again as an argument: the metaobject is shared by every value of the type, so a meta-method is told which object it is being asked about. The .^ form does this for you automatically. (For name the argument happens to be ignored, but passing it is the correct, general form — some meta-methods do use it.)

The same applies to other meta-methods you may meet, such as .^methods, which lists the methods a value responds to.

WHO

WHO returns the package that a name belongs to — the table of symbols defined in that namespace:

say Int.WHO.^name; # Stash

A Stash (a symbol-table hash) becomes useful when you work with modules, where it lets you look up the names a module defines. We will return to it in the section about modules; for now, it is enough to know that WHO exists and what it represents.

Course navigation

Using VAR   |   Describe a variable


💪 Or jump directly to the exercises in this section.