Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

merge features/code from perl5i; take tests from perl5i; doc revamp

non-working development version; tests are still failing
  • Loading branch information...
commit 7f01ddf0e9f3a1d00220900aa2004dda85bf3157 1 parent 4bac932
@scrottie authored
Showing with 414 additions and 59 deletions.
  1. +371 −56 Core.pm
  2. +15 −3 t/added.t
  3. +28 −0 t/scalar.t
View
427 Core.pm
@@ -14,14 +14,30 @@ package autobox::Core;
# autobox::Core::open would have to know how to handle $_[0] being undef and
# assigning the open'ed handle into $_[0].
+# TODO:
+
+# o. perl6now.com is bjorked
+# o. regenerate README
+# o. docs should show @arr->whatever syntax that works in non-antique autoboxes.
+# v/ steal perl5i's tests too
+# o. steal perl5i's docs too
+# o. IO::Any?
+# o. "appending the user-supplied arguments allows autobox::Core options to be overridden" -- document this if we haven't already
+# o. more Hash::Util methods?
+# o. Hash/Merge/Simple.pm and autobox/List/Util.pm aren't "CORE"
+# o. dammit, List::Util and Scalar::Util are core but they aren't CORE. split this up into two modules?
+# o. "If this goes over well, I'll make L<Langauge::Functional> a dependency and expose its function as methods on the correct data types. Or maybe I will do this anyway."
+# o. C<each> on hashes. There is no good reason it is missing.
+
use 5.8.0;
use strict;
use warnings;
-our $VERSION = '0.6';
+our $VERSION = '0.07';
-use base qw(autobox);
+use autobox;
+use base 'autobox';
# appending the user-supplied arguments allows autobox::Core options to be overridden
# or extended in the same statement e.g.
@@ -38,26 +54,28 @@ sub import {
=head1 NAME
-autobox::Core - Methods for core built-in functions in primitive types
+autobox::Core - Core functions exposed as methods in primitive types
=head1 SYNOPSIS
use autobox::Core;
- "Hello, World\n"->uc()->print();
+ "Hello, World\n"->uc->print;
=head1 DESCRIPTION
-Methods wrapping F<perl>'s built-in functions for minipulating numbers, strings, arrays,
-hashes, and code references. It can be handy to use built-in functions as methods to avoid
-messy dereferencing syntaxes and parentheses pile ups.
+The L<autobox> module lets you call methods on primitive datatypes such as
+scalars and arrays.
+
+L<autobox::CORE> defines methods for core operations such as C<join>, C<print>,
+most everything in L<perlfunc>, some things from L<Scalar::Util> and
+L<List::Util>, and some Perl 5 versions of methods taken from Perl 6.
-L<autobox> lets you call methods in scalars that aren't object references.
-Numbers, strings, scalars containing numbers, scalars containing strings,
-array references, hash references, and code references all work as objects.
-L<autobox> adds this feature to L<perl> but does not itself provide any
-methods to call. That is left to the user or another module. For example,
-this module.
+These methods expose as methods the built-in functions for minipulating
+numbers, strings, arrays, hashes, and code references.
+
+It can be handy to use built-in functions as methods to avoid
+messy dereferencing syntaxes and parentheses pile ups.
F<autobox::Core> is what you'd call a I<stub> module. It is merely glue, presenting
existing functions with a new interface. Most of the methods read like
@@ -75,35 +93,144 @@ All of the functions listed in L<perldoc> under the headings:
"Functions for list data",
and "Functions for SCALARs or strings", plus a few taken from other sections
and documented below.
-Some things expected in Perl 6, such as C<last>, C<elems>, and C<curry>, have been thrown in.
-For use in conjuction with L<Perl6::Contexts>, C<flatten> explicitly flattens an array.
-Functions have been defined for numeric operations.
+Methods from L<Scalar::Util> and L<List::Util> were thrown in.
+Some things expected in Perl 6, such as C<last> (C<last_idx>), C<elems>, and C<curry>, have been thrown in.
+C<flatten> explicitly flattens an array.
+Functions such as C<add> have been defined for numeric operations.
-Of the built-in stuff, the things you use most often on data are all implemented.
Here's a small sample:
- print [10, 20, 30, 40, 50]->pop(), "\n";
- print [10, 20, 30, 40, 50]->shift(), "\n";
+ print [10, 20, 30, 40, 50]->pop, "\n";
+ print [10, 20, 30, 40, 50]->shift, "\n";
my $arrref = [10, 20, 30];
my $lala;
- $lala = "Lalalalala\n"; print "chomp: ", $lala->chomp(), ' ', $lala, "\n";
- $lala = "Lalalalala\n"; print "lcfirst: ", $lala->lcfirst(), ' ', $lala, "\n";
+ $lala = "Lalalalala\n"; print "chomp: ", $lala->chomp, ' ', $lala, "\n";
+ $lala = "Lalalalala\n"; print "lcfirst: ", $lala->lcfirst, ' ', $lala, "\n";
my $hashref = { foo => 10, bar => 20, baz => 30, qux => 40 };
print "hash keys: ", join ' ', $hashref->keys(), "\n";
-Besides those sections of L<perlfunc>, I've implemented
+Of the built-in stuff, only a few stragglers such as C<srand> were excluded.
+
+C<title_case>, C<center>, C<ltrim>, C<rtrim>, and C<trim> were taken from L<perl5i>.
+
+=head3 Reference Related Methods
+
+Besides the "Functions for SCALARs" section of L<perlfunc>, the following were implemented, where they
+make sense:
C<tie>,
C<tied>,
C<ref>,
C<undef>,
C<bless>,
-and C<vec>, where they make sense.
+and C<vec>.
C<tie>, C<tied>, and C<undef> don't work on code references, and C<bless> doesn't work on non-reference
-scalars.
+scalars (okay, that's no longer true).
C<quotemeta> works on non-reference scalars, along with C<split>, C<m>, and C<s> for regular expression operations.
+C<ref> is the same as the C<ref> keyword in that it tells you what kind of a reference something is if it's a
+reference; XXX there's currently no counterpart to the C<< \ >> operator, which takes something and gives you
+a reference to it.
+
+C<times> executes a coderef a given number of times:
+
+ 5->times(sub { print "hi\n"}); # XXX likely to change but it's in the code so bloody doc it so I have incentive to rethink it
+
+=head3 Scalar String Related Methods
+
+C<chomp>, C<chop>, C<chr>, C<crypt>, C<index>, C<lc>, C<lcfirst>, C<length>, C<ord>,
+ C<pack>, C<reverse>, C<rindex>, C<sprintf>, C<substr>,
+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<undef> assigns C<undef> to the value. It is not a test.
+XXX for some reason, there's no C<defined>.
+
+=head3 Scalar Number Related Methods
+
+
+sub abs ($) { CORE::abs($_[0]) }
+sub atan2 ($) { CORE::atan2($_[0], $_[1]) }
+sub cos ($) { CORE::cos($_[0]) }
+sub exp ($) { CORE::exp($_[0]) }
+sub int ($) { CORE::int($_[0]) }
+sub log ($) { CORE::log($_[0]) }
+sub oct ($) { CORE::oct($_[0]) }
+sub hex ($) { CORE::hex($_[0]); }
+sub rand ($) { CORE::rand($_[0]) }
+sub sin ($) { CORE::sin($_[0]) }
+sub sqrt ($) { CORE::sqrt($_[0]) }
+
+# functions for array creation
+sub to ($$) { $_[0] < $_[1] ? [$_[0]..$_[1]] : [CORE::reverse $_[1]..$_[0]]}
+sub upto ($$) { [ $_[0]..$_[1] ] }
+sub downto ($$) { [ CORE::reverse $_[1]..$_[0] ] }
+
+# just weird, but cool
+sub times ($&) { for (0..$_[0]-1) { $_[1]->($_); }; $_[0]; }
+# suggested but bombs test
+#sub times ($;&) {
+# if ($_[1]) {
+# for (0..$_[0]-1) { $_[1]->($_); }; $_[0];
+# } else {
+# 0..$_[0]-1
+# }
+#}
+
+# doesn't minipulate scalars but works on scalars
+
+sub print ($;@) { CORE::print @_; }
+sub say ($;@) { CORE::print @_, "\n"}
+
+# operators that work on scalars:
+
+sub concat ($;@) { CORE::join '', @_; }
+sub strip ($) { my $s = CORE::shift; $s =~ s/^\s+//; $s =~ s/\s+$//; $s }
+
+# operator schizzle
+sub add($$) { $_[0] + $_[1]; }
+sub and($$) { $_[0] && $_[1]; }
+sub band($$) { $_[0] & $_[1]; }
+sub bor($$) { $_[0] | $_[1]; }
+sub bxor($$) { $_[0] ^ $_[1]; }
+sub cmp($$) { $_[0] cmp $_[1]; }
+sub dec($) { my $t = CORE::shift @_; --$t; }
+sub div($$) { $_[0] / $_[1]; }
+sub eq($$) { $_[0] eq $_[1]; }
+sub flip($) { ~$_[0]; }
+sub ge($$) { $_[0] ge $_[1]; }
+sub gt($$) { $_[0] gt $_[1]; }
+sub inc($) { my $t = CORE::shift @_; ++$t; }
+sub le($$) { $_[0] le $_[1]; }
+sub lshift($$) { $_[0] << $_[1]; }
+sub lt($$) { $_[0] lt $_[1]; }
+sub mod($$) { $_[0] % $_[1]; }
+sub mult($$) { $_[0] * $_[1]; }
+sub mcmp($$) { $_[0] <=> $_[1]; }
+sub ne($$) { $_[0] ne $_[1]; }
+sub neg($) { -$_[0]; }
+sub meq($$) { $_[0] == $_[1]; }
+sub mge($$) { $_[0] >= $_[1]; }
+sub mgt($$) { $_[0] > $_[1]; }
+sub mle($$) { $_[0] <= $_[1]; }
+sub mlt($$) { $_[0] < $_[1]; }
+sub mne($$) { $_[0] != $_[1]; }
+sub not($) { !$_[0]; }
+sub or($$) { $_[0] || $_[1]; }
+sub pow($$) { $_[0] ** $_[1]; }
+sub rpt($$) { $_[0] x $_[1]; }
+sub rshift($$) { $_[0] >> $_[1]; }
+sub sub($$) { $_[0] - $_[1]; }
+sub xor($$) { $_[0] ^ $_[1]; }
+
+XXX round this section out
+
+=head3 Array Methods
my $arr = [ 1 .. 10 ];
$arr->undef;
@@ -112,7 +239,7 @@ Array references can tell you how many elements they contain and the index of th
my $arr = [ 1 .. 10 ];
print '$arr contains ', $arr->size,
- ' elements, the last having an index of ', $arr->last, "\n";
+ ' elements, the last having an index of ', $arr->last_idx, "\n";
Array references have a C<flatten> method to dump their elements.
This is the same as C<< @{$array_ref} >>.
@@ -120,14 +247,6 @@ This is the same as C<< @{$array_ref} >>.
my $arr = [ 1 .. 10 ];
print join " -- ", $arr->flatten, "\n";
-Under L<Perl6::Contexts>, you'll often need to write code equivalent to the follow:
-
- use Perl6::Contexts;
- use autobox::Core;
-
- my @arr = ( 1 .. 10 );
- do_something(@arr->flatten);
-
Array references can be iterated on using C<for> and C<foreach>. Both take a code
reference as the body of the for statement.
C<foreach> passes the current element itself in each pass.
@@ -140,10 +259,17 @@ the current element, and then a reference to the array itself.
C<sum> is a toy poke at doing L<Language::Functional>-like stuff:
- print $arrref->sum(), "\n";
+ print $arrref->sum, "\n";
+
+XXX round this out
+
+ 1->to(5); # creates [1, 2, 3, 4, 5]
+ 1->upto(5); # creates [1, 2, 3, 4, 5]
+ 5->downto(5); # creates [5, 4, 3, 2, 1]
-If this goes over well, I'll make L<Langauge::Functional> a dependency and expose
-its function as methods on the correct data types. Or maybe I will do this anyway.
+These wrap the C<..> operator.
+
+=head3 Hash Methods
C<each> is like C<foreach> but for hash references. For each key in the hash,
the code reference is invoked with the key and the corresponding value as arguments:
@@ -189,13 +315,18 @@ C<add>, C<and>, C<band>, C<bor>, C<bxor>, C<cmp>, C<dec>, C<div>, C<eq>, C<flip>
That's it.
+XXX round this out
+
=head2 What's Missing?
Many operators. I'm tired. I'll do it in the morning. Maybe. Send me a patch.
Update: Someone sent me a patch for numeric operations.
+XXX document the numeric operator functions
+
File and socket operations are already implemented in an object-oriented fashion
-care of L<IO::Handle> and L<IO::Socket::INET>.
+care of L<IO::Handle>, L<IO::Socket::INET>, and L<IO::Any>.
+
Functions listed in the L<perlfunc> headings "System V interprocess communication functions",
"Fetching user and group info",
"Fetching network info",
@@ -212,8 +343,8 @@ modules, are keywords and not functions,
take no arguments,
or don't make sense as part of the string, number, array, hash, or code API.
C<srand> because you probably shouldn't be using it.
-C<each> on hashes.
-There is no good reason it is missing.
+
+C<each> on hashes. There is no good reason it is missing.
=head2 Autoboxing
@@ -443,6 +574,13 @@ The API is not yet stable -- Perl 6-ish things and local extensions are still be
=head1 HISTORY
+Version 0.8 adds the LICENSE section, as requested.
+Use C<< ~~ >> on C<< @array->grep >> if we're using 5.10 or newer.
+
+Version 0.7 uses autobox itself so you don't have to, as requested, and
+... oh hell. I started editing this to fix Schwern's reported v-string
+warning, but I'm not seeing it.
+
Version 0.6 propogates arguments to C<autobox> and doesn't require you to use
C<autobox>. I still can't test it and am applying patches blindly. Maybe I'll
drop the Hash::Util dep in the next version since it and Scalar::Util are
@@ -465,6 +603,15 @@ Version 0.2 rounded out the API and introduced the beginnings of functional-ish
Version 0.1 was woefully incomplete.
+=head1 COPYRIGHT AND LICENSE
+
+Copyright (C) 2009, 2010 by Scott Walters
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself, either Perl version 5.8.9 or,
+at your option, any later version of Perl 5 you may have available.
+
+
=head1 SEE ALSO
=over 1
@@ -475,6 +622,10 @@ Version 0.1 was woefully incomplete.
=item L<Perl6::Contexts>
+=item L<http://github.com/gitpan/autobox-Core>
+
+=item L<IO::Any>
+
=item Perl 6: L<< http://dev.perl.org/perl6/apocalypse/ >>.
=item (Shameless plug:) I<Perl 6 Now: The Core Ideas Illustrated with Perl 5>
@@ -564,6 +715,14 @@ sub downto ($$) { [ CORE::reverse $_[1]..$_[0] ] }
# just weird, but cool
sub times ($&) { for (0..$_[0]-1) { $_[1]->($_); }; $_[0]; }
+# suggested but bombs test
+#sub times ($;&) {
+# if ($_[1]) {
+# for (0..$_[0]-1) { $_[1]->($_); }; $_[0];
+# } else {
+# 0..$_[0]-1
+# }
+#}
# doesn't minipulate scalars but works on scalars
@@ -611,12 +770,76 @@ sub rshift($$) { $_[0] >> $_[1]; }
sub sub($$) { $_[0] - $_[1]; }
sub xor($$) { $_[0] ^ $_[1]; }
+# sub bless (\%$) { CORE::bless $_[0], $_[1] } # HASH, ARRAY, CODE already have a bless() and blessing a non-reference works (autobox finds the reference in the pad or stash!). "can't bless a non-referenc value" for non-reference lexical and package scalars. this would work for (\$foo)->bless but then, unlike arrays, we couldn't find the reference to the variable again later so there's not much point I can see.
+
+# from perl5i:
+
+
+sub title_case {
+ my ($string) = @_;
+ $string =~ s/\b(\w)/\U$1/g;
+ return $string;
+}
+
+
+sub center {
+ my ($string, $size, $char) = @_;
+ Carp::carp("Use of uninitialized value for size in center()") if !defined $size;
+ $size = defined($size) ? $size : 0;
+ $char = defined($char) ? $char : 0;
+
+ if (CORE::length $char > 1) {
+ my $bad = $char;
+ $char = CORE::substr $char, 0, 1;
+ Carp::carp("'$bad' is longer than one character, using '$char' instead");
+ }
+
+ my $len = CORE::length $string;
+
+ return $string if $size <= $len;
+
+ my $padlen = $size - $len;
+
+ # pad right with half the remaining characters
+ my $rpad = CORE::int( $padlen / 2 );
+
+ # bias the left padding to one more space, if $size - $len is odd
+ my $lpad = $padlen - $rpad;
+
+ return $char x $lpad . $string . $char x $rpad;
+}
+
+sub ltrim {
+ my ($string,$trim_charset) = @_;
+ $trim_charset = '\s' unless defined $trim_charset;
+ my $re = qr/^[$trim_charset]*/;
+ $string =~ s/$re//;
+ return $string;
+}
+
+
+sub rtrim {
+ my ($string,$trim_charset) = @_;
+ $trim_charset = '\s' unless defined $trim_charset;
+ my $re = qr/[$trim_charset]*$/;
+ $string =~ s/$re//;
+ return $string;
+}
+
+
+sub trim {
+ my $charset = $_[1];
+
+ return rtrim(ltrim($_[0], $charset), $charset);
+}
#
# HASH
#
package autobox::Core::HASH;
+use Carp 'croak';
+
# Functions for real %HASHes
# "delete", "each", "exists", "keys", "values"
@@ -659,27 +882,82 @@ use Hash::Util;
sub lock_keys (\%) { Hash::Util::lock_keys(%{$_[0]}); $_[0]; }
+# from perl5i
+
+sub flip {
+ croak "Can't flip hash with references as values"
+ if grep { CORE::ref } CORE::values %{$_[0]};
+
+ return { reverse %{$_[0]} };
+}
+
+sub merge {
+ require Hash::Merge::Simple;
+ Hash::Merge::Simple::merge(@_);
+}
+
#
# ARRAY
#
##############################################################################################
package autobox::Core::ARRAY;
+use constant FIVETEN => ($] >= 5.010);
+
+use Carp 'croak';
+
# Functions for list data
# "grep", "join", "map", "qw/STRING/", "reverse",
# "sort", "unpack"
-sub grep (\@&) { my $arr = CORE::shift; my $sub = CORE::shift; [ CORE::grep { $sub->($_) } @$arr ]; }
-sub join (\@$) { my $arr = CORE::shift; my $sep = CORE::shift; CORE::join $sep, @$arr; }
-sub map (\@&) { my $arr = CORE::shift; my $sub = shift; [ CORE::map { $sub->($_) } @$arr ]; }
-sub reverse (\@) { [ CORE::reverse @{$_[0]} ] }
-sub sort (\@;&) { my $arr = CORE::shift; my $sub = CORE::shift() || sub { $a cmp $b }; [ CORE::sort { $sub->($a, $b) } @$arr ]; }
+# at one moment, perl5i had this in it:
+
+#sub grep {
+# my ( $array, $filter ) = @_;
+# my @result = CORE::grep { $_ ~~ $filter } @$array;
+# return wantarray ? @result : \@result;
+#}
+
+sub grep {
+ no warnings 'redefine';
+ if(FIVETEN) {
+ eval '
+ # protect perl 5.8 from the alien, futuristic syntax of 5.10
+ *grep = sub {
+ my $arr = CORE::shift;
+ my $filter = CORE::shift;
+ my @result = CORE::grep { $_ ~~ $filter } @$arr;
+ return wantarray ? @result : \@result;
+ }
+ ' or croak $@;
+ } else {
+ *grep = sub {
+ my $arr = CORE::shift;
+ my $filter = CORE::shift;
+ my @result = CORE::grep { $filter->($_) } @$arr;
+ return wantarray ? @result : \@result;
+ };
+ }
+ autobox::Core::ARRAY::grep(@_);
+}
+
+# last version: sub map (\@&) { my $arr = CORE::shift; my $sub = shift; [ CORE::map { $sub->($_) } @$arr ]; }
+
+sub map {
+ my( $array, $code ) = @_;
+ my @result = CORE::map { $code->($_) } @$array;
+ return wantarray ? @result : \@result;
+}
+
+sub join { my $arr = CORE::shift; my $sep = CORE::shift; CORE::join $sep, @$arr; }
+sub reverse { [ CORE::reverse @{$_[0]} ] }
+sub sort { my $arr = CORE::shift; my $sub = CORE::shift() || sub { $a cmp $b }; [ CORE::sort { $sub->($a, $b) } @$arr ]; }
# functionalish stuff
-sub sum (\@) { my $arr = CORE::shift; my $res = 0; $res += $_ foreach(@$arr); $res; }
-sub mean(\@) { my $arr = CORE::shift; my $res = 0; $res += $_ foreach(@$arr); $res/@$arr; }
-sub var(\@)
+sub sum { my $arr = CORE::shift; my $res = 0; $res += $_ foreach(@$arr); $res; }
+sub mean { my $arr = CORE::shift; my $res = 0; $res += $_ foreach(@$arr); $res/@$arr; }
+sub var
{
my $arr = CORE::shift;
my $mean = 0;
@@ -730,15 +1008,52 @@ sub ref (\@) { CORE::ref $_[0] }
# perl 6-ish extensions to Perl 5 core stuff
-sub first(\@) { my $arr = CORE::shift; $arr->[0]; }
-sub last (\@) { my $arr = CORE::shift; $#$arr; }
+# sub first(\@) { my $arr = CORE::shift; $arr->[0]; } # old, incompat version
+
+sub first {
+ # from perl5i, modified
+ # XXX needs test. take from perl5i?
+ no warnings "redefine";
+ if(FIVETEN) {
+ eval '
+ # protect perl 5.8 from the alien, futuristic syntax of 5.10
+ *first = sub {
+ my ( $array, $filter ) = @_;
+ # Deep recursion and segfault (lines 90 and 91 in first.t) if we use
+ # the same elegant approach as in grep().
+ if ( ! $filter ) {
+ return $array->[0];
+ } elsif ( CORE::ref $filter eq "Regexp" ) {
+ my @res = List::Util::first( sub { $_ ~~ $filter }, @$array );
+ return wantarray ? @res : \@res;
+ } else {
+ return List::Util::first( sub { $filter->() }, @$array );
+ }
+ };
+ ' or croak $@;
+ } else {
+ *first = sub {
+ my ( $array, $filter ) = @_;
+ if ( ! $filter ) {
+ return $array->[0];
+ }
+ my @res = List::Util::first( sub { $filter->() }, @$array );
+ return wantarray ? @res : \@res;
+ };
+ }
+ autobox::Core::ARRAY::first(@_);
+}
+
+sub last_idx (\@) { my $arr = CORE::shift; $#$arr; }
sub size (\@) { my $arr = CORE::shift; CORE::scalar @$arr; }
sub elems (\@) { my $arr = CORE::shift; CORE::scalar @$arr; } # Larry announced it would be elems, not size
sub length (\@) { my $arr = CORE::shift; CORE::scalar @$arr; }
+# XXXXXXXXX arr->slice(n, n, n)
+
# misc
-sub each (\@$) {
+sub each {
# same as foreach(), apo12 mentions this
my $arr = CORE::shift; my $sub = CORE::shift;
foreach my $i (@$arr) {
@@ -746,27 +1061,27 @@ sub each (\@$) {
}
}
-sub foreach (\@$) {
+sub foreach {
my $arr = CORE::shift; my $sub = CORE::shift;
foreach my $i (@$arr) {
$sub->($i);
}
}
-sub for (\@$) {
+sub for {
my $arr = CORE::shift; my $sub = CORE::shift;
for(my $i = 0; $i < $#$arr; $i++) {
$sub->($i, $arr->[$i], $arr);
}
}
-sub print (\@) { my $arr = CORE::shift; my @arr = @$arr; CORE::print "@arr"; }
-sub say (\@) { my $arr = CORE::shift; my @arr = @$arr; CORE::print "@arr\n"; }
+sub print { my $arr = CORE::shift; my @arr = @$arr; CORE::print "@arr"; }
+sub say { my $arr = CORE::shift; my @arr = @$arr; CORE::print "@arr\n"; }
# local
-sub elements (\@) { ( @{$_[0]} ) }
-sub flatten (\@) { ( @{$_[0]} ) }
+sub elements { ( @{$_[0]} ) }
+sub flatten { ( @{$_[0]} ) }
##############################################################################################
View
18 t/added.t
@@ -1,6 +1,5 @@
use Test::More;
-BEGIN { plan tests => 65 };
-use autobox;
+BEGIN { plan tests => 68 };
use autobox::Core;
#####################################################################
@@ -96,9 +95,12 @@ $a = 10->downto(1);
ok($a->[0] == 10 && $a->[@$a-1] == 1);
$a = 1;
-10->times(sub {$a++});
+ok(10->times(sub {$a++}) == 10);
ok($a == 11);
+#$a = 1;
+#$a++ for 10->times;
+#ok($a == 11);
#####################################################################
# Hashes
@@ -157,3 +159,13 @@ $a = $a->uniq;
ok($a->count(1) == 1 && $a->count(4) == 1);
ok($a->first == 0);
+ok($a->first(sub { m/3/ }) == 3);
+
+$a = 1->to(10);
+$a->unshift(100);
+ok($a->sum == 155);
+
+
+
+
+
View
28 t/scalar.t
@@ -0,0 +1,28 @@
+#!perl
+
+use Test::More 'no_plan';
+use autobox::Core;
+
+is( "this is a test"->title_case, 'This Is A Test');
+is( "this is a test"->lc->title_case, 'This Is A Test');
+
+is( "thIS is a teST"->title_case, 'ThIS Is A TeST');
+is( "thIS is a teST"->lc->title_case, 'This Is A Test');
+
+is( ' testme'->ltrim, 'testme' );
+is( ' testme'->rtrim, ' testme' );
+is( ' testme'->trim, 'testme' );
+
+is( 'testme '->ltrim, 'testme ' );
+is( 'testme '->rtrim, 'testme' );
+is( 'testme '->trim, 'testme' );
+
+is( ' testme '->ltrim, 'testme ' );
+is( ' testme '->rtrim, ' testme' );
+is( ' testme '->trim, 'testme' );
+
+is( '--> testme <--'->ltrim("-><"), ' testme <--' );
+is( '--> testme <--'->rtrim("-><"), '--> testme ' );
+is( '--> testme <--'->trim("-><"), ' testme ' );
+
+is( ' --> testme <--'->trim("-><"), ' --> testme ' );
Please sign in to comment.
Something went wrong with that request. Please try again.