Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Dumping Perl 6 (AmsterdamX.pm)

1,242 views

Published on

A data structure dumper for Perl 6, a talk given at AmsterdamX.pm.

Published in: Technology
  • Be the first to comment

Dumping Perl 6 (AmsterdamX.pm)

  1. 1. Dumping Perl 6 AmsterdamX.pm, 8 Jun 2017
  2. 2. ThePerlReview•www.theperlreview.com DumpingPerl6 Sponsored in part by a grant from
  3. 3. ThePerlReview•www.theperlreview.com DumpingPerl6 Plain my $rx = rx/ <[ a .. z ]> <[ 1 .. 9 ]> /; my $string = ':::abc123::'; my $match = $string ~~ $rx; put $match; c1
  4. 4. ThePerlReview•www.theperlreview.com DumpingPerl6 .gist my $rx = rx/ <[ a .. z ]> <[ 1 .. 9 ]> /; my $string = ':::abc123::'; my $match = $string ~~ $rx; say $match; # put $match.gist 「c1」
  5. 5. ThePerlReview•www.theperlreview.com DumpingPerl6 .perl my $rx = rx/ <[ a .. z ]> <[ 1 .. 9 ]> /; my $string = ':::abc123::'; my $match = $string ~~ $rx; put $match.perl; Match.new(list => (), made => Any, pos => 7, hash => Map.new(()), orig => ":::abc123::", from => 5)
  6. 6. ThePerlReview•www.theperlreview.com DumpingPerl6 Pretty::Printer use Pretty::Printer; # From Jeff Goff my $rx = rx/ <[ a .. z ]> <[ 1 .. 9 ]> /; my $string = ':::abc123::'; my $match = $string ~~ $rx; Pretty::Printer.new.pp: $match; Any
  7. 7. ThePerlReview•www.theperlreview.com DumpingPerl6 method _pp($ds,$depth) { my Str $str; given $ds.WHAT { when Hash { $str ~= self.Hash($ds,$depth) } when Array { $str ~= self.Array($ds,$depth) } when Pair { $str ~= self.Pair($ds,$depth) } when Str { $str ~= $ds.perl } when Numeric { $str ~= ~$ds } when Nil { $str ~= q{Nil} } when Any { $str ~= q{Any} } } return self.indent-string($str,$depth); }
  8. 8. ThePerlReview•www.theperlreview.com DumpingPerl6 PrettyDump use PrettyDump; my $rx = rx/ <[ a .. z ]> <[ 1 .. 9 ]> /; my $string = ':::abc123::'; my $match = $string ~~ $rx; put PrettyDump.new.dump: $match;
  9. 9. ThePerlReview•www.theperlreview.com DumpingPerl6 Match.new( :from(5), :hash(Map.new()), :list($()), :made(Mu), :orig(":::abc123::"), :pos(7), :to(7) )
  10. 10. ThePerlReview•www.theperlreview.com DumpingPerl6 By Type
  11. 11. ThePerlReview•www.theperlreview.com DumpingPerl6 method _pp($ds,$depth) { my Str $str; given $ds.WHAT { # Check more derived types first. when Match { $str ~= self.Match($ds,$depth) } when Hash { $str ~= self.Hash($ds,$depth) } when Array { $str ~= self.Array($ds,$depth) } when Map { $str ~= self.Map($ds,$depth) } when List { $str ~= self.List($ds,$depth) } when Pair { $str ~= self.Pair($ds,$depth) } when Str { $str ~= $ds.perl } when Numeric { $str ~= ~$ds } when Nil { $str ~= q{Nil} } when Any { $str ~= q{Any} } } return self.indent-string($str,$depth); }
  12. 12. ThePerlReview•www.theperlreview.com DumpingPerl6 Per Class
  13. 13. ThePerlReview•www.theperlreview.com DumpingPerl6 class SomeClass { … method PrettyDump ( $pretty, $ds, $depth ) { … } } my $pretty = PrettyDump.new; my $some-object = SomeClass.new; put $pretty.dump: $some-object;
  14. 14. ThePerlReview•www.theperlreview.com DumpingPerl6 use PrettyDump; my $pretty = PrettyDump.new; my Int $a = 137; put $pretty.dump: $a; my $b = $a but role { method PrettyDump ( $pretty, $depth = 0 ) { "({self.^name}) {self}"; } }; put $pretty.dump: $b;
  15. 15. ThePerlReview•www.theperlreview.com DumpingPerl6 method dump ( $ds, $depth = 0 ) { put "In dump. Got ", $ds.^name; my Str $str; if $ds.can: 'PrettyDump' { $str ~= $ds.PrettyDump: self; } elsif $ds ~~ Numeric { $str ~= self.Numeric: $ds, $depth; } elsif self.can: $ds.^name { my $what = $ds.^name; $str ~= self."$what"( $ds, $depth ); } else { die "Could not handle " ~ $ds.perl; } return self.indent-string: $str, $depth; }
  16. 16. ThePerlReview•www.theperlreview.com DumpingPerl6 Per Object
  17. 17. ThePerlReview•www.theperlreview.com DumpingPerl6 Decorate the Dumper my $pretty = PrettyDump.new; class SomeClass { … } my $handler = sub ( $pretty, $ds, Int $depth = 0 ) { ... } $pretty.add-handler: 'SomeClass', $handler; put $pretty.dump: $SomeClass-object;
  18. 18. 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; } …
  19. 19. 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; } …
  20. 20. 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; }
  21. 21. ThePerlReview•www.theperlreview.com DumpingPerl6 Not Data::Dumper • Does not produce eval-able code • Does not know what it has already seen • Missing a few formatting features
  22. 22. ThePerlReview•www.theperlreview.com DumpingPerl6 What else might it do? • Handle more data types • More configurable formatting options • Formatting hooks • Send email
  23. 23. ThePerlReview•www.theperlreview.com DumpingPerl6 Questions

×