Commits

Toby Inkster committed 1b256ef

Data::Printer stuff

Comments (0)

Files changed (3)

examples/triangle.pl

+use strict;
+use Data::Printer;
+use MooX::Struct
+	Triangle => [qw/ $a! $b! $c! /],
+	Quad     => [qw/ $a! $b! $c! $d! /],
+	Point    => [qw/ +x +y /],
+;
+
+my $triangle = Triangle[
+	Point[0, 0],
+	Point[1, 1],
+	Point[1, 0],
+];
+
+my $square = Quad[
+	Point[0, 0],
+	Point[0, 1],
+	Point[1, 1],
+	Point[1, 0],
+];
+
+my %shapes = ( three_sided => [$triangle] , four_sided => [$square] );
+p %shapes;

lib/MooX/Struct.pm

 	return $class->SUPER::BUILDARGS(@_);
 }
 
+my $done = 0;
+sub _data_printer
+{
+	require Data::Printer::Filter;
+	require Term::ANSIColor;
+	my $self   = shift;
+	
+	my @values = map { scalar Data::Printer::p($_) } @$self;
+
+	if (grep /\n/, @values)
+	{
+		return sprintf(
+			"%s[\n\t%s,\n]",
+			Term::ANSIColor::colored($self->TYPE, 'bright_yellow'),
+			join(qq[,\n\t], map { s/\n/\n\t/gm; $_ } @values),
+		);
+	}
+	else
+	{
+		return sprintf(
+			'%s[ %s ]',
+			Term::ANSIColor::colored($self->TYPE, 'bright_yellow'),
+			join(q[, ], @values),
+		);
+	}
+}
+
 BEGIN {
 	package MooX::Struct::Processor;
 	
 
 Moo internal fu.
 
+=item C<_data_printer>
+
+Automatic pretty printing with L<Data::Printer>.
+
+ use Data::Printer;
+ use MooX::Struct Point => [qw/ +x +y /];
+ my $origin = Point[];
+ p $origin;
+
 =back
 
 With the exception of C<FIELDS> and C<TYPE>, any of these can be overridden

meta/changes.pret

 		item "New method 'CLONE'."^^Addition;
 		item "Provide Point[$x,$y] style constructor."^^Addition;
 		item "Overload a lot of operations on structs."^^Addition;
+		item "Special Data::Printer support."^^Addition;
 	].