packageMooX::Struct;use5.008;usestrict;usewarnings;useutf8;BEGIN{$MooX::Struct::AUTHORITY='cpan:TOBYINK';$MooX::Struct::VERSION='0.005';}useMoo1.000000;useObject::ID0qw( );useScalar::Does0qw( does );useoverloadq[""]=>'TO_STRING',q[bool]=>sub {1},q[@{}]=>'TO_ARRAY',q[=]=>'CLONE',fallback=>1;METHODS:{nowarnings;sub OBJECT_ID{goto\&Object::ID::object_id};sub FIELDS{qw()};sub TYPE{+undef};sub TO_ARRAY{[map{;$_[0]->$_}$_[0]->FIELDS]};sub TO_HASH{+{map{;$_=>$_[0]->$_}$_[0]->FIELDS}};sub TO_STRING{joinq[ ],@{$_[0]->TO_ARRAY}};sub CLONE{my$s=shift;ref($s)->new(%{$s->TO_HASH},@_)};};sub BUILDARGS{my$class=shift;my@fields=$class->FIELDS;if(@_==1anddoes($_[0],'ARRAY')andnotdoes($_[0],'HASH')){my@values=@{$_[0]};Carp::confess("too many values passed to constructor (expected @fields); stopped")unless@fields>=@values;nowarnings;return+{map{$fields[$_]=>$values[$_];}0..$#values}}my$hashref=$class->SUPER::BUILDARGS(@_);my%tmp=map{$_=>1}keys%$hashref;delete$tmp{$_}for@fields;if(my@unknown=sortkeys%tmp){Carp::confess("unknown keys passed to constructor (@unknown); stopped");}return$hashref;}sub EXTEND{my($invocant,@args)=@_;my$base=ref($invocant)||$invocant;my$processor='MooX::Struct::Processor'->new;while(@argsand$args[0]=~ /^-(.+)$/){$processor->flags->{lc($1)}=!!shift;}my$subname=undef;$subname=${shift@args}ifref($args[0])eq'SCALAR';my$new_class=$processor->make_sub($subname,[-extends=>[$base],@args],)->();return$new_classunlessref$invocant;bless$invocant=>$new_class;}# This could do with some improvement from a Data::Printer expert.#my$done=0;sub _data_printer{requireData::Printer::Filter;requireTerm::ANSIColor;my$self=shift;my@values=map{scalarData::Printer::p($_)}@$self;if(grep/\n/,@values){returnsprintf("%s[\n\t%s,\n]",Term::ANSIColor::colored($self->TYPE||'struct','bright_yellow'),join(qq[,\n\t],map{s/\n/\n\t/gm;$_}@values),);}else{returnsprintf('%s[ %s ]',Term::ANSIColor::colored($self->TYPE||'struct','bright_yellow'),join(q[, ],@values),);}}BEGIN{packageMooX::Struct::Processor;sub _uniq{my%seen;grep{not$seen{$_}++}@_};useMoo1.000000;useCarp0qw( confess );useData::OptList0qw( );useSub::Install0qw( install_sub );useScalar::Does0qw( does blessed looks_like_number );usenamespace::cleanqw( );useB::Hooks::EndOfScopeqw( on_scope_end );hasflags=>(is=>'ro',isa=>sub {die"flags must be HASH"unlessdoes$_[0],'HASH'},default=>sub {+{}},);hasclass_map=>(is=>'ro',isa=>sub {die"class_map must be HASH"unlessdoes$_[0],'HASH'},default=>sub {+{}},);hasbase=>(is=>'ro',default=>sub {'MooX::Struct'},);hastrace=>(is=>'lazy',);sub _build_trace{$ENV{PERL_MOOX_STRUCT_TRACE}orshift->flags->{trace};}hastrace_handle=>(is=>'lazy',);sub _build_trace_handle{requireIO::Handle;\*STDERR;}my$counter=0;sub create_class{my$self=shift;my$klass=sprintf('%s::__ANON__::%04d',$self->base,++$counter);Moo->_set_superclasses($klass,$self->base);Moo->_maybe_reset_handlemoose($klass);if($self->trace){$self->trace_handle->printf("package %s;\nuse Moo;\n",$klass,);}return$klass;}sub process_meta{my($self,$klass,$name,$val)=@_;if($nameeq'-extends'or$nameeq'-isa'){my@parents=map{exists$self->class_map->{$_}?$self->class_map->{$_}->():$_}@$val;Moo->_set_superclasses($klass,@parents);Moo->_maybe_reset_handlemoose($klass);if($self->trace){$self->trace_handle->printf("extends qw(%s)\n",join(q[ ]=>@parents),);}returnmap{$_->can('FIELDS')?$_->FIELDS:()}@parents;}elsif($nameeq'-with'){requireMoo::Role;Moo::Role->apply_roles_to_package($klass,@$val);Moo->_maybe_reset_handlemoose($klass);if($self->trace){$self->trace_handle->printf("with qw(%s)\n",join(q[ ]=>@$val),);}returnmap{my$role=$_;grep{notref$_}@{$Moo::Role::INFO{$role}{attributes}}}@$val;}else{confess("option '$name' unknown");}return;}sub process_method{my($self,$klass,$name,$coderef)=@_;install_sub{into=>$klass,as=>$name,code=>$coderef,};if($self->trace){$self->trace_handle->printf("sub %s { ... }\n",$name,);if($self->flags->{deparse}){requireB::Deparse;my$code=B::Deparse->new(qw(-q -si8T))->coderef2text($coderef);$code=~s/^/# /mig;$self->trace_handle->printf("$code\n");}}return;}sub process_spec{my($self,$klass,$name,$val)=@_;my%spec=(is=>($self->flags->{rw}?'rw':'ro'),(does($val,'ARRAY')?@$val:(does($val,'HASH')?%$val:())),);if($name=~ /^(.+)\!$/){$name=$1;$spec{required}=1;}if($name=~ /^\@(.+)/){$name=$1;$spec{isa}||=sub {die"wrong type for '$name' (not arrayref)"unlessdoes($_[0],'ARRAY');};}elsif($name=~ /^\%(.+)/){$name=$1;$spec{isa}||=sub {die"wrong type for '$name' (not hashref)"unlessdoes($_[0],'HASH');};}elsif($name=~ /^\+(.+)/){$name=$1;$spec{isa}||=sub {die"wrong type for '$name' (not number)"unlesslooks_like_number($_[0]);};$spec{default}||=sub {0}unless$spec{required};}elsif($name=~ /^\$(.+)/){$name=$1;$spec{isa}||=sub {my$ref=ref($_[0]);die"wrong type for '$name' (should not be arrayref or hashref)"if$refeq'ARRAY'||$refeq'HASH';};}return($name,\%spec);}sub process_attribute{my($self,$klass,$name,$val)=@_;my$spec;($name,$spec)=$self->process_spec($klass,$name,$val);if($self->trace){requireData::Dumper;my$spec_str=Data::Dumper->new([$spec])->Terse(1)->Indent(0)->Dump;$spec_str=~s/(^\{)|(\}$)//g;$self->trace_handle->printf("has %s => (%s);\n",$name,$spec_str,);if($self->flags->{deparse}and$spec->{isa}){requireB::Deparse;my$code=B::Deparse->new(qw(-q -si8T))->coderef2text($spec->{isa});$code=~s/^/# /mig;$self->trace_handle->printf("$code\n");}}Moo->_constructor_maker_for($klass)->register_attribute_specs($name,$spec);Moo->_accessor_maker_for($klass)->generate_method($klass,$name,$spec);Moo->_maybe_reset_handlemoose($klass);return$name;}# returns a list of "fields" resulting from the argumentsub process_argument{my$self=shift;my($klass,$name,$val)=@_;return$self->process_meta(@_)if$name=~ /^-/;return$self->process_method(@_)ifdoes($val,'CODE');return$self->process_attribute(@_);}sub make_sub{my($self,$subname,$proto)=@_;returnsub (;$) {1if$]<5.014;# bizarre, but necessary!if(ref$proto)# inflate!{my$klass=$self->create_class;my@fields=_uniqmap{$self->process_argument($klass,@$_)}@{Data::OptList::mkopt($proto)};$self->process_method($klass,FIELDS=>sub {@fields});$self->process_method($klass,TYPE=>sub {$subname})ifdefined$subname;$proto=$klass;}return$proto->new(@_)if@_;return$proto;}}sub process{my$self=shift;my$caller=shift;while(@_and$_[0]=~ /^-(.+)$/){$self->flags->{lc($1)}=!!shift;}foreachmy$arg(@{Data::OptList::mkopt(\@_)}){my($subname,$details)=@$arg;$details=[]unlessdefined$details;$self->class_map->{$subname}=$self->make_sub($subname,$details);install_sub{into=>$caller,as=>$subname,code=>$self->class_map->{$subname},};}on_scope_end{namespace::clean->clean_subroutines($caller,keys%{$self->class_map},);};}};sub import{my$caller=caller;my$class=shift;"$class\::Processor"->new->process($caller,@_);}noMoo;1;__END__=head1 NAMEMooX::Struct - make simple lightweight record-like structures that make sounds like cows=head1 SYNOPSIS use MooX::Struct Point => [ 'x', 'y' ], Point3D => [ -extends => ['Point'], 'z' ], ; my $origin = Point3D->new( x => 0, y => 0, z => 0 ); # or... my $origin = Point3D[ 0, 0, 0 ];=head1 DESCRIPTIONMooX::Struct allows you to create cheap struct-like classes for your datausing L<Moo>.While similar in spirit to L<MooseX::Struct> and L<Class::Struct>, MooX::Struct has a somewhat different usage pattern. Rather than providingyou with a C<struct> keyword which can be used to define structs, youdefine all the structs as part of the C<use> statement. This means theyhappen at compile time.A struct is just an "anonymous" Moo class. MooX::Struct creates this classfor you, and installs a lexical alias for it in your namespace. Thus yourmodule can create a "Point3D" struct, and some other module can too, andthey won't interfere with each other. All struct classes inherit fromMooX::Struct.Arguments for MooX::Struct are key-value pairs, where keys are the structnames, and values are arrayrefs. use MooX::Struct Person => [qw/ name address /], Company => [qw/ name address registration_number /];The elements in the array are the attributes for the struct (which will becreated as read-only attributes), however certain array elements are treatedspecially.=over=item *As per the example in the L</SYNOPSIS>, C<< -extends >> introduces a list ofparent classes for the struct. If not specified, then classes inherit fromMooX::Struct itself.Structs can inherit from other structs, or from normal classes. If inheritingfrom another struct, then you I<must> define both in the same C<use> statement.Inheriting from a non-struct class is discouraged. # Not like this. use MooX::Struct Point => [ 'x', 'y' ]; use MooX::Struct Point3D => [ -extends => ['Point'], 'z' ]; # Like this. use MooX::Struct Point => [ 'x', 'y' ], Point3D => [ -extends => ['Point'], 'z' ], ;=item *Similarly C<< -with >> consumes a list of roles.=item *If an attribute name is followed by a coderef, this is installed as amethod instead. use MooX::Struct Person => [ qw( name age sex ), greet => sub { my $self = shift; CORE::say "Hello ", $self->name; }, ];But if you're defining methods for your structs, then you've possibly missedthe point of them.=item *If an attribute name is followed by an arrayref, these are used to set theoptions for the attribute. For example: use MooX::Struct Person => [ name => [ is => 'ro', required => 1 ] ];Using the C<init_arg> option would probably break stuff. Don't do that.=item *Attribute names may be "decorated" with prefix and postfix "sigils". The prefixsigils of C<< @ >> and C<< % >> specify that the attribute isa arrayref orhashref respectively. (Blessed arrayrefs and hashrefs are accepted; as areobjects which overload C<< @{} >> and C<< %{} >>.) The prefix sigil C<< $ >>specifies that the attribute value must not be an unblessed arrayref or hashref.The prefix sigil C<< + >> indicates the attribute is a number, and providesa default value of 0, unless the attribute is required. The postfix sigilC<< ! >> specifies that the attribute is required. use MooX::Struct Person => [qw( $name! @children )]; Person->new(); # dies, name is required Person->new( # dies, children should be arrayref name => 'Bob', children => 2, );=backPrior to the key-value list, some additional flags can be given. These beginwith hyphens. The flag C<< -rw >> indicates that attributes should beread-write rather than read-only. use MooX::Struct -rw, Person => [ qw( name age sex ), greet => sub { my $self = shift; CORE::say "Hello ", $self->name; }, ];Flags C<< -trace >> and C<< -deparse >> may be of use debugging.=head2 Instantiating StructsThere are two supported methods of instatiating structs. You can use atraditional class-like constructor with named parameters: my $point = Point->new( x => 1, y => 2 );Or you can use the abbreviated syntax with positional parameters: my $point = Point[ 1, 2 ];If you know about Moo and peek around in the source code for this module,then I'm sure you can figure out additional ways to instantiate them, butthe above are the only supported two.When inheritance or roles have been used, it might not always be clear whatorder the positional parameters come in (though see the documentation for theC<FIELDS> below), so the traditional class-like style may be preferred.=head2 MethodsStructs are objects and thus have methods. You can define your own methodsas described above. MooX::Struct's built-in methods will always obey theconvention of being in ALL CAPS (except in the case of C<_data_printer>).By using lower-case letters to name your own methods, you can avoidnaming collisions.The following methods are currently defined. Additionally all the standardPerl (C<isa>, C<can>, etc) and Moo (C<new>, C<does>, etc) methods areavailable.=over=item C<OBJECT_ID> Returns a unique identifier for the object.=item C<FIELDS> Returns a list of fields associated with the object. For the C<Point3D> structin the SYNPOSIS, this would be C<< 'x', 'y', 'z' >>.The order the fields are returned in is equal to the order they must be suppliedfor the positional constructor.=item C<TYPE>Returns the type name of the struct, e.g. C<< 'Point3D' >>.=item C<TO_HASH>Returns a reference to an unblessed hash where the object's fields are thekeys and the object's values are the hash values.=item C<TO_ARRAY>Returns a reference to an unblessed array where the object's values are thearray items, in the same order as listed by C<FIELDS>.=item C<TO_STRING>Joins C<TO_ARRAY> with whitespace. This is not necessarily a brilliantstringification, but easy enough to overload: use MooX::Struct Point => [ qw( x y ), TO_STRING => sub { sprintf "(%d, %d)"), $_[0]->x, $_[0]->y; }, ] ;=item C<CLONE>Creates a shallow clone of the object. =item C<EXTEND>An exverimental feature.Extend a class or object with additional attributes, methods, etc. This methodtakes almost all the same arguments as C<use MooX::Struct>, albeit with someslight differences. use MooX::Struct Point => [qw/ +x +y /]; my $point = Point[2, 3]; $point->EXTEND(-rw, q/+z/); # extend an object $point->can('z'); # true my $new_class = Point->EXTEND('+z'); # extend a class my $point_3d = $new_class->new( x => 1, y => 2, z => 3 ); $point_3d->TYPE; # Point ! my $point_4d = $new_class->EXTEND(\"Point4D", '+t'); $point_4d->TYPE; # Point4D my $origin = Point[]->EXTEND(-with => [qw/ Math::Role::Origin /]);This feature has been included mostly because it's easy to implement on topof the existing code for processing C<use MooX::Struct>. Some subsets ofthis functionality are sane, such as the ability to add traits to an object.Others (like the ability to add a new uninitialized, read-only attribute toan existing object) are less sensible.=item C<BUILDARGS>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;=backWith the exception of C<FIELDS> and C<TYPE>, any of these can be overriddenusing the standard way of specifying methods for structs.=head2 OverloadingMooX::Struct overloads stringification and array dereferencing. Objects alwaysevaluate to true in a boolean context. (Even if they stringify to the emptystring.)=head1 BUGSPlease report any bugs toL<http://rt.cpan.org/Dist/Display.html?Queue=MooX-Struct>.=head1 SEE ALSOL<Moo>, L<MooX::Struct::Util>, L<MooseX::Struct>, L<Class::Struct>.=head1 AUTHORToby Inkster E<lt>tobyink@cpan.orgE<gt>.=head1 COPYRIGHT AND LICENCEThis software is copyright (c) 2012 by Toby Inkster.This is free software; you can redistribute it and/or modify it underthe same terms as the Perl 5 programming language system itself.=head1 DISCLAIMER OF WARRANTIESTHIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIEDWARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OFMERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.