🦋64. What does gist do in Perl 6?

🦋64. What does gist do in Raku?

N. B. Perl 6 has been renamed to Raku. Click to read more.


When you print an object, say, as say $x, Perl 6 calls the gist method. This method is defined for all built-in types: for some of them, it calls the Str method, for some the perl method, for some types it makes the string representation somehow differently.

Let us see how you can use the method to create your own variant:

class X {
    has $.value;

    method gist {
        '[' ~ $!value ~ ']'
    }
}

my $x = X.new(value => 42);

say $x; # [42]
$x.say; # [42]

When you call say, the program prints a number in square brackets: [42].

Please notice that the interpolation inside double-quoted strings is using Str, not gist. You can see it here:

say $x.Str; # X<140586830040512>
say "$x";   # X<140586830040512>

If you need a custom interpolation, redefine the Str method:

class X {
    has $.value;

    method gist {
        '[' ~ $!value ~ ']'
    }
    method Str {
        '"' ~ $!value ~ '"'
    }
}

my $x = X.new(value => 42);

say $x;     # [42]
$x.say;     # [42]

say $x.Str; # "42"
say "$x";   # "42"

Now, we got the desired behaviour.

2 thoughts on “🦋64. What does gist do in Perl 6?”

Leave a Reply