Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing objects in Perl 6

Tags:

raku

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?

like image 870
Eugene Barsky Avatar asked Oct 08 '18 20:10

Eugene Barsky


1 Answers

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.

like image 92
Elizabeth Mattijsen Avatar answered Jan 04 '23 17:01

Elizabeth Mattijsen