autobox::Core

autobox::Core contains built-in functions exposed as methods in primitive types.
Download

autobox::Core Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Scott Walters
  • Publisher web site:
  • http://search.cpan.org/~swalters/Math-Preference-SVD-0.01/lib/Math/Preference/SVD.pm

autobox::Core Tags


autobox::Core Description

autobox::Core contains built-in functions exposed as methods in primitive types. autobox::Core contains built-in functions exposed as methods in primitive types.SYNOPSIS use autobox; use autobox::Core; "Hello, Worldn"->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($_) }. 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.What's Implemented?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, elems, and curry, have been thrown in. For use in conjuction with Perl6::Contexts, flatten explicitly flattens an array. Functions 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 ->pop(), "n"; print ->shift(), "n"; my $arrref = ; my $lala; $lala = "Lalalalalan"; print "chomp: ", $lala->chomp(), ' ', $lala, "n"; $lala = "Lalalalalan"; 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, along with split, m, and s for regular expression operations. my $arr = ; $arr->undef;Array references can tell you how many elements they contain and the index of their last element: my $arr = ; print '$arr contains ', $arr->size, ' elements, the last having an index of ', $arr->last, "n";Array references have a flatten method to dump their elements. This is the same as @{$array_ref}. my $arr = ; print join " -- ", $arr->flatten, "n";Under Perl6::Contexts, you'll often need to write code equivalent to the follow: use Perl6::Contexts; use autobox; use autobox::Core; my @arr = ( 1 .. 10 ); do_something(@arr->flatten);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. my $arr = ; $arr->foreach(sub { print $_, "n" }); $arr->for(sub { die unless $_ == $_->] });sum is a toy poke at doing Language::Functional-like stuff: 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: my $hashref = { foo => 10, bar => 20, baz => 30, quux => 40 }; $hashref->each(sub { print $_, ' is ', $_, "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. my ($street_number, $street_name, $apartment_number) = "1234 Robin Drive #101"->m(qr{(d+) (.*)(?: #(d+))?})->elements; print "$street_number $street_name $apartment_numbern";split is called on a non-reference scalar with the regular expression passed in. This is done for consistency with m and s. print "10, 20, 30, 40"->split(qr{, ?})->elements, "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;These work on numbers:add, and, band, bor, bxor, cmp, dec, div, eq, flip, ge, gt, inc, le, lshift, lt, mod, mult, mcmp, ne, neg, meq, mge, mgt, mle, mlt, mne, not, or, pow, rpt, rshift, sub, xor.Requirements:· Perl Requirements: · Perl


autobox::Core Related Software