NAME

MooseX::Lists - treat arrays and hashes as lists

SYNOPSIS

   package Stuff;

   use Moose;
   use MooseX::Lists;

   has_list a => ( is => 'rw', isa => 'Array');
   has_list h => ( is => 'rw', isa => 'Hash' );

   has_list same_as_a => ( is => 'rw' );

   ...

   my $s = Stuff-> new(
   	a => [1,2,3],
	h => { a => 1, b => 2 }
   );

   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

DESCRIPTION

Provides asymmetric list access for arrays and hashes

METHODS

has_list

Replacement for has, with exactly same syntax, expect for isa, which must begin either with ArrayRef or HashRef. If isa is omitted, array is assumed.

When a method is declared with has_list, internally it is a normal perl array or hash. The method behaves differently if called in scalar or list context. See below for details.

ArrayRef

In get-mode, behaves like auto_deref: in scalar context, returns direct reference to the array, list context, returns defereenced array.

In set-mode behaves asymmetrically: if passed one argument, and this argument is an arrayref, treats it as an arrayref, otherwise dereferences the arguments and creates a new arrayref, which is stored internally. I.e. the only way to clear the array is to call -method([]) >.

HashRef

In get-mode, behaves like auto_deref: in scalar context, returns direct reference to the hash, list context, returns defereenced hash.

In set-mode behaves asymmetrically: if passed one argument, and this argument is a hashref, treats it as a hashref, otherwise dereferences the arguments and creates a new hashref, which is stored internally. I.e. the only way to clear the hash is to call -method({}) >.

AUTHOR

Dmitry Karasik, <dmitry@karasik.eu.org>.

THANKS

Karen Etheridge, Jesse Luehrs, Stevan Little.