From: Tomas Doran Date: Thu, 11 Feb 2010 02:02:27 +0000 (+0000) Subject: Add first and last X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=cc6691de77a8de4b8e8a8524d391b7b3ad5a9be4;p=gitmo%2FMoose-Autobox.git Add first and last --- 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 e43c893..de9a3cc 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 { @@ -235,6 +243,10 @@ This is a role to describe operations on the Array type. =item B +=item B + +=item B + =back =head2 Indexed 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';