| Perl6-Perl documentation | Contained in the Perl6-Perl distribution. |
Perl6::Perl - $obj->perl just like $obj.perl in Perl 6
# As UNIVERSAL method
use Perl6::Perl;
use Foo::Bar;
my $baz = Foo::Bar->new();
my $bazz = eval( $baz->perl ); # $bazz is a copy of $baz
# As subroutine so you can apply to non-objects
use Perl6::Perl qw/perl/; # explicitly import
perl $scalar;
perl \@array;
perl \%hash;
perl \*GLOB;
perl sub{ $_[0] + 1 };
# Ruby's p
p $complex_object;
In Perl 6, everything is an object and every object comes with the
.perl method that returns the eval()uable representation
thereof. This module does just that.
Since Perl 5 is already shipped with Data::Dumper, this module
makes use of it; In fact $obj->perl is just a wrapper to
Dumper($obj) with options slightly different from Data::Dumper's
default.
This module also comes with p, which is analogous to that of ruby;
It is simply sub p{ print perl(@_), "\n" }. But you save a lot of
key strokes -- even more concise than say @_.perl .
Though p is not Perl6's spec, I couldn't resist adding this to this module because so many people envy Ruby for it :).
Perl6::Perl uses the following values as default:
1 so you can serialize coderef.
1 so no $VAR1 = appears.
1 so you can safely inspect binary data as well as Unicode characters.
2 if the object is a coderef, 0 otherwise.
You can override these by feeding Data::Dumper options as follows;
$obj->perl(purity => 1); # if the object contains circular reference.
Note you can use all lowercaps here.
None by default. perl and p are exported on demand.
Dan Kogai, <dankogai@dan.co.jp>
Copyright (C) 2006 by Dan Kogai
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.
| Perl6-Perl documentation | Contained in the Perl6-Perl distribution. |
package Perl6::Perl; # # $Id: Perl.pm,v 0.1 2006/12/23 23:12:17 dankogai Exp dankogai $ # use 5.008001; use strict; use warnings; use Data::Dumper (); use Scalar::Util qw/blessed/; use base 'Exporter'; our $VERSION = sprintf "%d.%02d", q$Revision: 0.1 $ =~ /(\d+)/g; our @EXPORT = qw(); our %EXPORT_TAGS = ( 'all' => [ qw( p perl ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); # Default Values our %DD_Default = ( Deparse => 1, Terse => 1, Useqq => 1, ); sub perl { my $self = shift; return $self unless ref $self; my $dd = Data::Dumper->new( [$self] ); $dd->$_( $DD_Default{$_} ) for keys %DD_Default; $dd->Indent( blessed($self) && $self->isa('CODE') ? 2 : ref $self eq 'CODE' ? 2 : 0 ); while ( my ( $k, $v ) = splice( @_, 0, 2 ) ){ $k = ucfirst $k; $dd->$k($v); } return $dd->Dump; } sub p{ print perl(@_), "\n" } *UNIVERSAL::perl = \&perl; # Preloaded methods go here. 1; __END__