package ArrayBox; use 5.008001; use overload ( nomethod => sub { die 'future reserved' }, fallback => 1 ); use strict; use warnings; our $VERSION = '0.00_05'; $VERSION = eval $VERSION; sub new { goto \&ArrayBox::Core::_new } sub DESTROY { goto \&ArrayBox::Core::_DESTROY } sub clone { goto \&ArrayBox::Core::_clone } sub swap { goto \&ArrayBox::Core::_swap } sub each { goto \&ArrayBox::Core::_each } sub grep { goto \&ArrayBox::Core::_grep } sub map { goto \&ArrayBox::Core::_map } sub push { goto \&ArrayBox::Core::_push } sub unshift { goto \&ArrayBox::Core::_unshift } sub all { goto \&ArrayBox::Core::_all } sub count { goto \&ArrayBox::Core::_count } sub pop { goto \&ArrayBox::Core::_pop } sub shift { goto \&ArrayBox::Core::_shift } sub one { goto \&ArrayBox::Core::_one } { package ArrayBox::Core; use Data::Dumper qw( Dumper ); use Scalar::Util qw( blessed refaddr ); my %array_ref; sub _new { my ($class, @array) = @_; my $self = bless \ do { my $anon_scalar }, ref $class || $class; $array_ref{ refaddr $self } = \@array; return $self; } sub _DESTROY { delete $array_ref{ refaddr $_[0] } } sub _clone { my ($self) = @_; my $clone = do { my $VAR1; eval Dumper $self }; $array_ref{ refaddr $clone } = do { my $VAR1; eval Dumper $array_ref{ refaddr $self } }; return $clone; } sub _swap { my ($self, $object) = @_; ($array_ref{ refaddr $self }, $array_ref{ refaddr $object }) = ($array_ref{ refaddr $object }, $array_ref{ refaddr $self }); return $self; } sub _each { my ($self, $sub) = @_; for ( @{ $array_ref{ refaddr $self } } ) { $sub->(); } return $self; } sub _grep { my ($self, $filter) = @_; my $clone = _clone($self); my $array_ref = $array_ref{ refaddr $clone }; @$array_ref = grep { $filter->() } @$array_ref; return $clone; } sub _map { my ($self, $filter) = @_; my $clone = _clone($self); my $array_ref = $array_ref{ refaddr $clone }; @$array_ref = map { $filter->() } @$array_ref; return $clone; } sub _push { my ($self, @values) = @_; push @{ $array_ref{ refaddr $self } }, @values; return $self; } sub _unshift { my ($self, @values) = @_; unshift @{ $array_ref{ refaddr $self } }, @values; return $self; } sub _all { @{ $array_ref{ refaddr $_[0] } } } sub _count { scalar @{ $array_ref{ refaddr $_[0] } } } sub _pop { pop @{ $array_ref{ refaddr $_[0] } } } sub _shift { shift @{ $array_ref{ refaddr $_[0] } } } sub _one { $array_ref{ refaddr $_[0] }->[ $_[1] or 0 ] } } sub even { my $i = 0; $_[0]->grep( sub { $i++ % 2 == 0 } ) } sub odd { my $i = 0; $_[0]->grep( sub { $i++ % 2 == 1 } ) } sub eq { my $r = $_[1]; my $i = 0; $_[0]->grep( sub { $i++ == $r } ) } sub first { $_[0]->eq(0) } sub last { $_[0]->eq( $_[0]->count - 1 ) } sub in { my ($self, @reqs) = @_; my $i = 0; return $self->grep( sub { my $i = $i++; CORE::grep { $i == $_ } @reqs } ); } 1; __END__ sub slice { my ($self, $start, $end) = @_; return $self->ge($start) if CORE::not defined $end; return $self->grep( sub { $start <= $_[0] and $_[0] < $end } ); } sub le { my $max = $_[1]; $_[0]->grep( sub { $_[0] <= $max } ) } sub ge { my $min = $_[1]; $_[0]->grep( sub { $_[0] >= $min } ) } sub lt { my $max = $_[1]; $_[0]->grep( sub { $_[0] < $max } ) } sub gt { my $min = $_[1]; $_[0]->grep( sub { $_[0] > $min } ) } 1; __END__ # Below is stub documentation for your module. You'd better edit it! =head1 NAME ArrayBox - Perl extension for blah blah blah =head1 SYNOPSIS use ArrayBox; blah blah blah =head1 DESCRIPTION Stub documentation for ArrayBox, created by h2xs. It looks like the author of the extension was negligent enough to leave the stub unedited. Blah blah blah. =head2 EXPORT None by default. =head1 SEE ALSO Mention other useful documentation such as the documentation of related modules or operating system documentation (such as man pages in UNIX), or any relevant external documentation such as RFCs or standards. If you have a mailing list set up for your module, mention it here. If you have a web site set up for your module, mention it here. =head1 AUTHOR Yuji Tamashiro, Eyuji@tamashiro.orgE =head1 COPYRIGHT AND LICENSE Copyright (C) 2009 by Yuji Tamashiro This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut