Subject: | [Patch]: add support for %hash->slice |
Patch includes tests, documentation and some minor cleanup.
chocolateboy
Subject: | autobox-Core-0.7-hash-slice.patch |
diff -ru autobox-Core-0.7/Core.pm autobox-Core-0.7-slice/Core.pm
--- autobox-Core-0.7/Core.pm 2010-03-05 07:07:22.000000000 +0000
+++ autobox-Core-0.7-slice/Core.pm 2010-03-08 01:41:47.000000000 +0000
@@ -36,7 +36,6 @@
our $VERSION = '0.7';
-use autobox;
use base 'autobox';
# appending the user-supplied arguments allows autobox::Core options to be overridden
@@ -138,9 +137,17 @@
C<uc>, C<ucfirst>, C<unpack>, C<quotemeta>, C<vec>, C<undef>, C<m>, C<nm>, C<s>, C<split>.
C<eval>, C<system>, and C<backtick>.
-C<m> matches: C<< $foo->m(/bar/) >> corresponds to C<< $foo =~ m/bar/ >>.
C<nm> corresponds to C<< !~ >>.
-C<s> corresponds to C<< =~ s/// >>.
+
+C<m> is C<< m// >>. C<< $foo->m(/bar/) >> corresponds to C<< $foo =~ m/bar/ >>. C<s> is C<< s/// >>.
+To use C<m> and C<s>, pass a regular expression created with C<< qr// >> and specify its flags
+as part of the regular expression using the C<< (?imsx-imsx) >> syntax documented in L<perlre>.
+C<m> returns an array reference so that things such as C<map> and C<grep> may be called on the result.
+
+ my ($street_number, $street_name, $apartment_number) =
+ "1234 Robin Drive #101"->m(qr{(\d+) (.*)(?: #(\d+))?})->elements;
+
+ print "$street_number $street_name $apartment_number\n";
C<undef> assigns C<undef> to the value. It is not a test.
XXX for some reason, there's no C<defined>.
@@ -264,16 +271,15 @@
There is currently no way to have the elements sorted before they are handed to the
code block. If someone requests a way of passing in a sort criteria, I'll implement it.
-C<m> is C<< m// >> and C<s> is C<< s/// >>. These work on scalars.
-Pass a regular expression created with C<< qr// >> and specify flags to the regular expression
-as part of the regular expression using the C<< (?imsx-imsx) >> syntax documented in L<perlre>.
-C<m> returns an array reference so that things such as C<map> and C<grep> may be called on the result.
-
- my ($street_number, $street_name, $apartment_number) =
- "1234 Robin Drive #101"->m(qr{(\d+) (.*)(?: #(\d+))?})->elements;
+C<slice> takes a list of hash keys and returns the corresponding values e.g.
- print "$street_number $street_name $apartment_number\n";
+ my %hash = (
+ one => 'two',
+ three => 'four',
+ five => 'six'
+ );
+ print %hash->slice(qw(one five))->join(' and '); # prints "two and six"
=head3 Code Methods
@@ -845,7 +851,7 @@
# Functions for real %HASHes
# "delete", "each", "exists", "keys", "values"
-sub delete (\%@) { my $hash = CORE::shift; my @res = (); CORE::foreach(@_) { push @res, CORE::delete $hash->{$_}; } CORE::wantarray ? @res : \@res }
+sub delete (\%@) { my $hash = CORE::shift; my @res = (); CORE::foreach(@_) { push @res, CORE::delete $hash->{$_}; } wantarray ? @res : \@res }
sub exists (\%$) { my $hash = CORE::shift; CORE::exists $hash->{$_[0]}; }
sub keys (\%) { [ CORE::keys %{$_[0]} ] }
sub values (\%) { [ CORE::values %{$_[0]} ] }
@@ -878,6 +884,11 @@
sub undef ($) { $_[0] = {} }
+sub slice {
+ my ($h, @keys) = @_;
+ wantarray ? @{$h}{@keys} : [ @{$h}{@keys} ];
+}
+
# okey, ::Util stuff should be core
use Hash::Util;
diff -ru autobox-Core-0.7/t/added.t autobox-Core-0.7-slice/t/added.t
--- autobox-Core-0.7/t/added.t 2010-03-05 06:13:31.000000000 +0000
+++ autobox-Core-0.7-slice/t/added.t 2010-03-08 01:46:36.000000000 +0000
@@ -1,5 +1,9 @@
-use Test::More;
-BEGIN { plan tests => 69 };
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 73;
use autobox::Core;
#####################################################################
@@ -106,6 +110,8 @@
# Hashes
#####################################################################
my $h = {a => 1, b => 2, c => 3};
+my %h = %$h;
+
ok($h->at('b') == 2);
ok($h->get('c') == 3);
@@ -126,6 +132,15 @@
"flattening gets us all the contents",
);
+my @slice = $h->slice(qw(a c));
+is_deeply(\@slice, [ 1, 3 ]);
+my $slice = $h->slice(qw(b c));
+is_deeply($slice, [ 2, 3 ]);
+@slice = %h->slice(qw(a c));
+is_deeply(\@slice, [ 1, 3 ]);
+$slice = %h->slice(qw(b c));
+is_deeply($slice, [ 2, 3 ]);
+
#####################################################################
# Array
#####################################################################