Eugene Barsky
Eugene Barsky

Reputation: 6012

Printing objects in Perl 6

Why do I get different results?

class Car {
  has $.wheels;
}

my $my_car = Car.new( wheels => 4 );

say  $my_car ;  # Car.new(wheels => 4)
say "$my_car";  # Car<94582644384824>
put  $my_car ;  # Car<94582644384824>

I suppose that in the 2nd and 3rd cases $my_car is stringified, but what does the result mean?

Upvotes: 8

Views: 123

Answers (1)

Elizabeth Mattijsen
Elizabeth Mattijsen

Reputation: 26969

The say command calls .gist on its argument. The put command calls .Str on its argument. And this also happens when you interpolate your object.

The default gist method looks at the public attributes of an object, and creates a string from that.

You can control how your object gets stringified by supplying your own Str method.

Upvotes: 11

Related Questions