20. ThePerlReview•www.theperlreview.com
DumpingPerl6
method dump ( $ds, Int $depth = 0 --> Str ) {
my Str $str = do {
# If the PrettyDump object has a user-defined handler
# for this type, prefer that one
if self.handles: $ds.^name {
self!handle: $ds, $depth;
}
# The object might have its own method to dump
# its structure
elsif $ds.can: 'PrettyDump' {
$ds.PrettyDump: self;
}
# If it's any sort of Numeric, we'll handle it
# and dispatch further
elsif $ds ~~ Numeric {
self!Numeric: $ds, $depth;
}
…
21. ThePerlReview•www.theperlreview.com
DumpingPerl6
# If we have a method name that matches the class, we'll
# use that.
elsif self.can: $ds.^name {
my $what = $ds.^name;
self."$what"( $ds, $depth );
}
# If the class inherits from something that we know
# about, use the most specific one that we know about
elsif $ds.^parents.grep(
{ self.can: $_.^name } ).elems > 0 {
my Str $str = '';
for $ds.^parents -> $type {
my $what = $type.^name;
next unless self.can( $what );
$str ~= self."$what"(
$ds, $depth, "{$ds.^name}.new(", ')' );
last;
}
$str;
}
…
22. ThePerlReview•www.theperlreview.com
DumpingPerl6
# If we're this far and the object has a .Str method,
# we'll use that:
elsif $ds.can: 'Str' {
"({$ds.^name}): " ~ $ds.Str;
}
# Finally, we'll put a placeholder method there
else {
"(Unhandled {$ds.^name})"
}
};
return self!indent-string: $str, $depth;
}