From: Ricardo Signes Date: Fri, 23 Apr 2010 20:22:58 +0000 (-0400) Subject: Add first and last X-Git-Tag: 0.11~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMoose-Autobox.git;a=commitdiff_plain;h=8b14b072ae9ab7d0786cdde1d97d743efbe414eb Add first and last Conflicts: lib/Moose/Autobox/Array.pm --- diff --git a/Changes b/Changes index 7d25803..7dc388a 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,7 @@ Revision history for Perl extension Moose::Autobox + - add first and last to Scalar and Array (t0m) + 0.10 - add each, each_key, each_value to Indexed (array and hash) (rjbs) - add split, words, lines to String (Sartak) diff --git a/lib/Moose/Autobox/Array.pm b/lib/Moose/Autobox/Array.pm index 1221e17..fb34f76 100644 --- a/lib/Moose/Autobox/Array.pm +++ b/lib/Moose/Autobox/Array.pm @@ -85,6 +85,14 @@ sub sort { [ CORE::sort { $sub->($a, $b) } @$array ]; } +sub first { + $_[0]->[0]; +} + +sub last { + $_[0]->[$#{$_[0]}]; +} + ## ::Indexed implementation sub at { @@ -246,7 +254,9 @@ This is a role to describe operations on the Array type. =item B -=item B +=item B + +=item B =back @@ -272,6 +282,8 @@ This is a role to describe operations on the Array type. =item B +=item B + =back =head2 List implementation diff --git a/lib/Moose/Autobox/Scalar.pm b/lib/Moose/Autobox/Scalar.pm index 41c33b5..6ebb9fe 100644 --- a/lib/Moose/Autobox/Scalar.pm +++ b/lib/Moose/Autobox/Scalar.pm @@ -7,6 +7,8 @@ with 'Moose::Autobox::String', 'Moose::Autobox::Number'; sub flatten { $_[0] } +sub first { $_[0] } +sub last { $_[0] } sub print { CORE::print $_[0] } sub say { CORE::print $_[0], "\n" } 1; @@ -45,6 +47,14 @@ Flattening a scalar just returns the scalar. This means that you can say: =back +=item B + +As per flatten. + +=item B + +As per flatten. + =head1 BUGS All complex software has bugs lurking in it, and this module is no diff --git a/t/011_first_last.t b/t/011_first_last.t new file mode 100644 index 0000000..ded7afe --- /dev/null +++ b/t/011_first_last.t @@ -0,0 +1,19 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 5; + +require_ok('Moose::Autobox'); + +use Moose::Autobox; + +my $string = 'foo'; +my $list = ['foo', 'bar']; + +is $string->first, 'foo'; +is $string->last, 'foo'; + +is $list->first, 'foo'; +is $list->last, 'bar';