Data::Visitor::Lite - an easy implementation of Data::Visitor::Callback
use Data::Visitor::Lite; my $visitor = Data::Visitor::Lite->new(@replacers); my $value = $visitor->visit({ # some structure });
Data::Visitor::Lite is an easy implementation of Data::Visitor::Callback
this is a constructor of Data::Visitor::Lite.
my $visitor = Data::Visitor::Lite->new( [ -implements => ['to_plain_object'] => sub { $_[0]->to_plain_object } ], [ -instance => 'Some::SuperClass' => sub { $_[0]->encode_to_utf8 } ] [ $replacer_type => $converter ] ); #or my $visitor2 = Data::Visitor::Lite->new(sub{ # callback all node of the structure }); my $value = $visitor->visit({ something });
Data::Visitor::Lite has many expressions to make replacer which is applied only specified data type.
If you want to convert only the objects that implements 'to_plain_object', you can write just following :
my $visitor = Data::Visitor::Lite->new( [ -implements => ['to_plain_object'] => sub { return $_[0]->to_plain_object; } ] );
it means it is easy to convert structures using duck typing.
"-instance" replacer type is able to create a converter for all instances of some class in the recursive structure.
my $visitor = Data::Visitor::Lite->new( [ -instance => 'Person' => sub{ $_[0]->nickname }] ); $visitor->visit({ master => Employer->new({ nickname => 'Kenji'}), slave => Employee->new({ nickname => 'Daichi'}); }); # { master => "Kenji", slave => 'Daichi'}
"-value" means not a reference and/or blessed object.
"-hashkey" means key string of the hash reference in the structure.
"-string" means hash keys and all string value in the structure.
"-object" means a reference and/or blessed object
the origin of other replace types is Data::Util.( e.g. glob_ref , scalar_ref, invocant , number ,integer and so on )
Daichi Hiroki <hirokidaichi {at} gmail.com>
Data::Visitor::Callback Data::Util
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Data::Visitor::Lite, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::Visitor::Lite
CPAN shell
perl -MCPAN -e shell install Data::Visitor::Lite
For more information on module installation, please visit the detailed CPAN module installation guide.