|
NAMEMooseX::Lists - treat arrays and hashes as listsSYNOPSISpackage Stuff; use Moose; use MooseX::Lists; has_list a => ( is => 'rw', isa => 'ArrayRef'); has_list h => ( is => 'rw', isa => 'HashRef' ); has_list same_as_a => ( is => 'rw' ); ... my $s = Stuff-> new( a => [1,2,3], h => { a => 1, b => 2 } ); Mixed list/scalar contexthas_list a => ( is => 'rw', isa => 'ArrayRef'); has_list h => ( is => 'rw', isa => 'HashRef' ); ... my @list = $s-> a; # ( 1 2 3 ) my $scalar = $s-> a; # [ 1 2 3 ] $s-> a(1,2,3); # 1 2 3 $s-> a([1,2,3]); # 1 2 3 $s-> a([]); # empty array $s-> a([[]]); # [] my %list = $s-> h; # ( a => 1, b => 2 ) my $sc = $s-> h; # { a => 1, b => 2 } $s-> h(1,2,3,4); # 1 2 3 4 $s-> h({1,2,3,4}); # 1 2 3 4 $s-> h({}); # empty hash Separated list/scalar contexthas_list a => ( is => 'rw', isa => 'ArrayRef', writer => 'wa', clearer => 'ca', ); has_list h => ( is => 'rw', isa => 'HashRef', writer => 'wh', clearer => 'ch', ); ... # reading part is identical to the above $s-> wa(1,2,3); # 1 2 3 $s-> wa([1,2,3]); # [1 2 3] $s-> wa(); # empty array $s-> ca(); # empty array $s-> wa([]); # [] $s-> wh(1,2,3,4); # 1 2 3 4 $s-> wh({1,2,3,4}); # error, odd number of elements $s-> wh(); # empty hash $s-> ch(); # empty hash DESCRIPTIONProvides asymmetric list access for arrays and hashes.The problem this module tries to solve is to provide an acceptable API for setting and accessing array and hash properties in list context. The problem in implementing such interface is when a handler accepts both arrays and arrayrefs, how to set an empty array, and differentiate between a set-call with an empty list or a get-call. Depending on the way a method is declared, two different setting modes are proposed. The first method, when "writer" is not explictly set (default), tries to deduce if it needs to dereference the arguments. It does so by checking if the argument is an arrayref. This means that the only way to clear an array or hash it to call it with "[]" or "{}", respectively. The second method is turned on if "writer" was explicitly specified, which means that if it is called with no arguments, this means an empty list. This method never dereferences array- and hashrefs. METHODS
AUTHORDmitry Karasik, <dmitry@karasik.eu.org>.THANKSKaren Etheridge, Jesse Luehrs, Stevan Little.
Visit the GSP FreeBSD Man Page Interface. |