Add first and last
Ricardo Signes [Fri, 23 Apr 2010 20:22:58 +0000 (16:22 -0400)]
Conflicts:

lib/Moose/Autobox/Array.pm

Changes
lib/Moose/Autobox/Array.pm
lib/Moose/Autobox/Scalar.pm
t/011_first_last.t [new file with mode: 0644]

diff --git a/Changes b/Changes
index 7d25803..7dc388a 100644 (file)
--- 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)
index 1221e17..fb34f76 100644 (file)
@@ -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<flatten_deep ($depth)>
 
-=item B<each_n_values ($n, $callback)>
+=item B<first>
+
+=item B<last>
 
 =back
 
@@ -272,6 +282,8 @@ This is a role to describe operations on the Array type.
 
 =item B<each_value>
 
+=item B<each_n_values ($n, $callback)>
+
 =back
 
 =head2 List implementation
index 41c33b5..6ebb9fe 100644 (file)
@@ -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<first>
+
+As per flatten.
+
+=item B<last>
+
+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 (file)
index 0000000..ded7afe
--- /dev/null
@@ -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';