autobox::Core - Perl built-in functions exposed as methods in primitive types
use autobox; use autobox::Core; "Hello, World\n"->uc()->print();
Methods wrapping perl's built-in functions for minipulating numbers, strings, arrays, hashes, and code references. It is handy to use built-in functions as methods to avoid messy dereferencing syntaxes and parenthesis pile ups.
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. autobox adds this feature to perl but does not itself provide any methods to call. That is left to the user or another module. For example, this module.
autobox::Core is what you'd call a stub module. It is merely glue, presenting existing functions with a new interface. Most of the methods read like sub hex ($) { hex($_[0]) }. Besides built-ins that operate on hashes, arrays, scalars, and code references, some Perl 6-ish things were thrown in, and some keyword like foreach have been turned into methods.
sub hex ($) { hex($_[0]) }
foreach
All of the functions listed in perldoc under the headings: "Functions for real @ARRAYs", "Functions for real %HASHes", "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 last, size, and curry, have been thrown in.
last
size
curry
Of the built-in stuff, the things you use most often on data are all implemented. A small sample:
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"; my $hashref = { foo => 10, bar => 20, baz => 30, qux => 40 }; print "hash keys: ", join ' ', $hashref->keys(), "\n";
Besides those sections of perlfunc, I've implemented tie, tied, ref, undef, bless, and vec, where they make sense. tie, tied, and undef don't work on code references, and bless doesn't work on non-reference scalars. quotemeta works on non-reference scalars.
tie
tied
ref
undef
bless
vec
quotemeta
my $arr = [ 1 .. 10 ]; $arr->undef;
Array references can tell you how many elements they contain and the index of their last element:
my $arr = [ 1 .. 10 ]; print '$arr contains ', $arr->size, ' elements, the last having an index of ', $arr->last, "\n";
Array references have an elements method to dump their elements. This is the same as @{$array_ref}.
elements
@{$array_ref}
my $arr = [ 1 .. 10 ]; print join " -- ", $arr->elements, "\n";
Array references can be iterated on using for and foreach. Both take a code reference as the body of the for statement. foreach passes the current element itself in each pass. for passes the index of the current element in to that code block, and then the current element, and then a reference to the array itself.
for
my $arr = [ 1 .. 10 ]; $arr->foreach(sub { print $_[0], "\n" }); $arr->for(sub { die unless $_[1] == $_[2]->[$_[0]] });
sum is a toy poke at doing Language::Functional-like stuff:
sum
print $arrref->sum(), "\n";
If this goes over well, I'll make Langauge::Functional a dependency and expose its function as methods on the correct data types. Or maybe I will do this anyway.
each is like 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:
each
my $hashref = { foo => 10, bar => 20, baz => 30, quux => 40 }; $hashref->each(sub { print $_[0], ' is ', $_[1], "\n" });
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.
m is m// and s is s///. These work on scalars. Pass a regular expression created with qr// and specify flags to the regular expression as part of the regular expression using the (?imsx-imsx) syntax documented in perlre. m returns an array reference so that things such as map and grep may be called on the result.
m
m//
s
s///
qr//
(?imsx-imsx)
map
grep
use autobox; use autobox::Core; my ($street_number, $street_name, $apartment_number) = "1234 Robin Drive #101"->m(qr{(\d+) (.*)(?: #(\d+))?})->elements; print "$street_number $street_name $apartment_number\n";
You may curry code references:
$adding_up_numbers = sub { my $first_number = shift; my $second_number = shift; return $first_number + $second_number; }; my $adding_five_to_numbers = $adding_up_numbers->curry(5); $adding_five_to_numbers->(20)->print; "\n"->print;
That's it.
Operators. I'm tired. I'll do it in the morning. Maybe. Send me a patch.
File and socket operations are already implemented in an object-oriented fashion care of IO::Handle and IO::Socket::INET. Functions listed in the perlfunc headings "System V interprocess communication functions", "Fetching user and group info", "Fetching network info", "Keywords related to perl modules", "Functions for processes and process groups", "Keywords related to scoping", "Time-related functions", "Keywords related to the control flow of your perl program", "Functions for filehandles, files, or directories", and "Input and output functions". These things are likely implemented in an object oriented fashion by other CPAN 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. srand because you probably shouldn't be using it. each on hashes. There is no good reason it is missing.
srand
Yes. Report them to the author, scott@slowass.net. This code is not well tested.
autobox.
Perl 6: http://dev.perl.org/perl6/apocalypse/.
Scott Walters, scott@slowass.net Thanks to chocolateboy for autobox and for the encouragement!
To install autobox::Core, copy and paste the appropriate command in to your terminal.
cpanm
cpanm autobox::Core
CPAN shell
perl -MCPAN -e shell install autobox::Core
For more information on module installation, please visit the detailed CPAN module installation guide.