.\" Automatically generated by Pod::Man 2.23 (Pod::Simple 3.14) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .ie \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .el \{\ . de IX .. .\} .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "autobox::Core 3pm" .TH autobox::Core 3pm "2011-09-26" "perl v5.12.4" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" autobox::Core \- Core functions exposed as methods in primitive types .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use autobox::Core; \& \& "Hello, World\en"\->uc\->print; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" The autobox module lets you call methods on primitive datatypes such as scalars and arrays. .PP autobox::CORE defines methods for core operations such as \f(CW\*(C`join\*(C'\fR, \f(CW\*(C`print\*(C'\fR, most everything in perlfunc, some things from Scalar::Util and List::Util, and some Perl 5 versions of methods taken from Perl 6. .PP These methods expose as methods the built-in functions for minipulating numbers, strings, arrays, hashes, and code references. .PP It can be handy to use built-in functions as methods to avoid messy dereferencing syntaxes and parentheses pile ups. .PP \&\fIautobox::Core\fR is what you'd call a \fIstub\fR module. It is mostly glue, presenting existing functions with a new interface. Most of the methods read like \&\f(CW\*(C`sub hex { hex($_[0]) }\*(C'\fR. Besides built-ins that operate on hashes, arrays, scalars, and code references, some Perl 6\-ish things were thrown in, and some keyword like \f(CW\*(C`foreach\*(C'\fR have been turned into methods. .SS "What's Implemented?" .IX Subsection "What's Implemented?" All of the functions listed in perldoc under the headings: \&\*(L"Functions for real \f(CW@ARRAYs\fR\*(R", \&\*(L"Functions for real \f(CW%HASHes\fR\*(R", \&\*(L"Functions for list data\*(R", and \*(L"Functions for SCALARs or strings\*(R", plus a few taken from other sections and documented below. Methods from Scalar::Util and List::Util were thrown in. Some things expected in Perl 6, such as \f(CW\*(C`last\*(C'\fR (\f(CW\*(C`last_idx\*(C'\fR), \f(CW\*(C`elems\*(C'\fR, and \f(CW\*(C`curry\*(C'\fR, have been thrown in. \&\f(CW\*(C`flatten\*(C'\fR explicitly flattens an array. Functions such as \f(CW\*(C`add\*(C'\fR have been defined for numeric operations. .PP Here's a small sample: .PP .Vb 2 \& print [10, 20, 30, 40, 50]\->pop, "\en"; \& print [10, 20, 30, 40, 50]\->shift, "\en"; \& \& my $arrref = [10, 20, 30]; \& \& my $lala; \& $lala = "Lalalalala\en"; print "chomp: ", $lala\->chomp, \*(Aq \*(Aq, $lala, "\en"; \& $lala = "Lalalalala\en"; print "lcfirst: ", $lala\->lcfirst, \*(Aq \*(Aq, $lala, "\en"; \& \& my $hashref = { foo => 10, bar => 20, baz => 30, qux => 40 }; \& \& print "hash keys: ", $hashref\->keys\->join(\*(Aq \*(Aq), "\en"; # or if you prefer... \& print "hash keys: ", join \*(Aq \*(Aq, $hashref\->keys(), "\en"; .Ve .PP Of the built-in stuff, only a few stragglers such as \f(CW\*(C`srand\*(C'\fR were excluded. .PP \fIScalar String Related Methods\fR .IX Subsection "Scalar String Related Methods" .PP \&\f(CW\*(C`concat\*(C'\fR is the \f(CW\*(C`.\*(C'\fR operator. .PP \&\f(CW\*(C`strip\*(C'\fR is not a built-in operator or function but is instead one of a number of user-defined convenience methods. \&\f(CW\*(C`strip\*(C'\fR strips out whitespace from the beginning and end of a string. This is redundant and subtly different from \f(CW\*(C`trim\*(C'\fR \s-1XXX\s0. .PP \&\f(CW\*(C`trim\*(C'\fR strips out whitespace from the beginning and end of a string. .PP \&\f(CW\*(C`title_case\*(C'\fR, \f(CW\*(C`center\*(C'\fR, \f(CW\*(C`ltrim\*(C'\fR, \f(CW\*(C`rtrim\*(C'\fR, and \f(CW\*(C`trim\*(C'\fR were taken from perl5i. .PP \&\f(CW\*(C`split\*(C'\fR is called on a non-reference scalar with the regular expression passed in. This is done for consistency with \f(CW\*(C`m\*(C'\fR and \f(CW\*(C`s\*(C'\fR. .PP .Vb 1 \& print "10, 20, 30, 40"\->split(qr{, ?})\->elements, "\en"; .Ve .PP \&\f(CW\*(C`chomp\*(C'\fR, \f(CW\*(C`chop\*(C'\fR, \f(CW\*(C`chr\*(C'\fR, \f(CW\*(C`crypt\*(C'\fR, \f(CW\*(C`index\*(C'\fR, \f(CW\*(C`lc\*(C'\fR, \f(CW\*(C`lcfirst\*(C'\fR, \f(CW\*(C`length\*(C'\fR, \f(CW\*(C`ord\*(C'\fR, \&\f(CW\*(C`pack\*(C'\fR, \f(CW\*(C`reverse\*(C'\fR, \f(CW\*(C`rindex\*(C'\fR, \f(CW\*(C`sprintf\*(C'\fR, \f(CW\*(C`substr\*(C'\fR, \&\f(CW\*(C`uc\*(C'\fR, \f(CW\*(C`ucfirst\*(C'\fR, \f(CW\*(C`unpack\*(C'\fR, \f(CW\*(C`quotemeta\*(C'\fR, \f(CW\*(C`vec\*(C'\fR, \f(CW\*(C`undef\*(C'\fR, \f(CW\*(C`m\*(C'\fR, \f(CW\*(C`nm\*(C'\fR, \f(CW\*(C`s\*(C'\fR, \f(CW\*(C`split\*(C'\fR. \&\f(CW\*(C`eval\*(C'\fR, \f(CW\*(C`system\*(C'\fR, and \f(CW\*(C`backtick\*(C'\fR. .PP \&\f(CW\*(C`nm\*(C'\fR corresponds to \f(CW\*(C`!~\*(C'\fR. .PP \&\f(CW\*(C`m\*(C'\fR is \f(CW\*(C`m//\*(C'\fR. \f(CW\*(C`$foo\->m(/bar/)\*(C'\fR corresponds to \f(CW\*(C`$foo =~ m/bar/\*(C'\fR. \f(CW\*(C`s\*(C'\fR is \f(CW\*(C`s///\*(C'\fR. To use \f(CW\*(C`m\*(C'\fR and \f(CW\*(C`s\*(C'\fR, pass a regular expression created with \f(CW\*(C`qr//\*(C'\fR and specify its flags as part of the regular expression using the \f(CW\*(C`(?imsx\-imsx)\*(C'\fR syntax documented in perlre. \&\f(CW\*(C`m\*(C'\fR returns an array reference so that things such as \f(CW\*(C`map\*(C'\fR and \f(CW\*(C`grep\*(C'\fR may be called on the result. .PP .Vb 2 \& my ($street_number, $street_name, $apartment_number) = \& "1234 Robin Drive #101"\->m(qr{(\ed+) (.*)(?: #(\ed+))?})\->elements; \& \& print "$street_number $street_name $apartment_number\en"; .Ve .PP \&\f(CW\*(C`undef\*(C'\fR assigns \f(CW\*(C`undef\*(C'\fR to the value. It is not a test. \&\s-1XXX\s0 for some reason, there's no \f(CW\*(C`defined\*(C'\fR. .PP \fIcenter()\fR .IX Subsection "center()" .PP .Vb 2 \& my $centered_string = $string\->center($length); \& my $centered_string = $string\->center($length, $character); .Ve .PP Centers \f(CW$string\fR between \f(CW$character\fR. \f(CW$centered_string\fR will be of length \f(CW$length\fR. .PP \&\f(CW$character\fR defaults to \*(L" \*(R". .PP .Vb 2 \& say "Hello"\->center(10); # " Hello "; \& say "Hello"\->center(10, \*(Aq\-\*(Aq); # "\-\-\-Hello\-\-"; .Ve .PP \&\f(CW\*(C`center()\*(C'\fR will never truncate \f(CW$string\fR. If \f(CW$length\fR is less than \f(CW\*(C`$string\->length\*(C'\fR it will just return \f(CW$string\fR. .PP .Vb 1 \& say "Hello"\->center(4); # "Hello"; .Ve .PP \fII/O\fR .IX Subsection "I/O" .PP \&\f(CW\*(C`print\*(C'\fR and \f(CW\*(C`say\*(C'\fR. .PP \fINumber Related Methods\fR .IX Subsection "Number Related Methods" .PP \&\f(CW\*(C`abs\*(C'\fR, \f(CW\*(C`atan2\*(C'\fR, \f(CW\*(C`cos\*(C'\fR, \f(CW\*(C`exp\*(C'\fR, \f(CW\*(C`int\*(C'\fR, \f(CW\*(C`log\*(C'\fR, \f(CW\*(C`oct\*(C'\fR, \f(CW\*(C`hex\*(C'\fR, \f(CW\*(C`rand\*(C'\fR, \f(CW\*(C`sin\*(C'\fR, and \f(CW\*(C`sqrt\*(C'\fR are named after the built-in functions of the same name. .PP Operators were given names as follows: \f(CW\*(C`add\*(C'\fR, \f(CW\*(C`and\*(C'\fR, \f(CW\*(C`band\*(C'\fR, \f(CW\*(C`bor\*(C'\fR, \f(CW\*(C`bxor\*(C'\fR, \f(CW\*(C`cmp\*(C'\fR, \f(CW\*(C`dec\*(C'\fR, \f(CW\*(C`div\*(C'\fR, \f(CW\*(C`eq\*(C'\fR, \f(CW\*(C`flip\*(C'\fR, \f(CW\*(C`ge\*(C'\fR, \f(CW\*(C`gt\*(C'\fR, \f(CW\*(C`inc\*(C'\fR, \f(CW\*(C`le\*(C'\fR, \f(CW\*(C`lshift\*(C'\fR, \f(CW\*(C`lt\*(C'\fR, \f(CW\*(C`mod\*(C'\fR, \f(CW\*(C`mult\*(C'\fR, \f(CW\*(C`mcmp\*(C'\fR, \f(CW\*(C`ne\*(C'\fR, \f(CW\*(C`neg\*(C'\fR, \f(CW\*(C`meq\*(C'\fR, \f(CW\*(C`mge\*(C'\fR, \f(CW\*(C`mgt\*(C'\fR, \f(CW\*(C`mle\*(C'\fR, \f(CW\*(C`mlt\*(C'\fR, \f(CW\*(C`mne\*(C'\fR, \f(CW\*(C`not\*(C'\fR, \f(CW\*(C`or\*(C'\fR, \f(CW\*(C`pow\*(C'\fR, \f(CW\*(C`rpt\*(C'\fR, \f(CW\*(C`rshift\*(C'\fR, \f(CW\*(C`sub\*(C'\fR, \f(CW\*(C`xor\*(C'\fR. .PP \&\f(CW\*(C`flip\*(C'\fR is \f(CW\*(C`~\*(C'\fR which is the binary (rather than boolean) \*(L"not\*(R". .PP \&\f(CW\*(C`lshift\*(C'\fR is \f(CW\*(C`<<\*(C'\fR and \f(CW\*(C`rshift\*(C'\fR is \f(CW\*(C`>>\*(C'\fR. .PP \&\f(CW\*(C`mge\*(C'\fR is \f(CW\*(C`>=\*(C'\fR. \f(CW\*(C` is \f(CW\*(C`<=\*(C'\fR. I'm not sure where the \*(L"m\*(R" came from. .PP \&\f(CW\*(C`sub\*(C'\fR is subtract, I think, but it should not be named the same as the anonymous subroutine constructor \s-1XXX\s0. .PP is_number .IX Subsection "is_number" .PP .Vb 1 \& $is_a_number = $thing\->is_number; .Ve .PP Returns true if \f(CW$thing\fR is a number understood by Perl. .PP .Vb 2 \& 12.34\->is_number; # true \& "12.34"\->is_number; # also true .Ve .PP is_positive .IX Subsection "is_positive" .PP .Vb 1 \& $is_positive = $thing\->is_positive; .Ve .PP Returns true if \f(CW$thing\fR is a positive number. .PP 0 is not positive. .PP is_negative .IX Subsection "is_negative" .PP .Vb 1 \& $is_negative = $thing\->is_negative; .Ve .PP Returns true if \f(CW$thing\fR is a negative number. .PP 0 is not negative. .PP is_integer .IX Subsection "is_integer" .PP .Vb 1 \& $is_an_integer = $thing\->is_integer; .Ve .PP Returns true if \f(CW$thing\fR is an integer. .PP .Vb 2 \& 12\->is_integer; # true \& 12.34\->is_integer; # false .Ve .PP is_int .IX Subsection "is_int" .PP A synonym for is_integer .PP is_decimal .IX Subsection "is_decimal" .PP .Vb 1 \& $is_a_decimal_number = $thing\->is_decimal; .Ve .PP Returns true if \f(CW$thing\fR is a decimal number. .PP .Vb 3 \& 12\->is_decimal; # false \& 12.34\->is_decimal; # true \& ".34"\->is_decimal; # true .Ve .PP That's it. .PP \fIReference Related Methods\fR .IX Subsection "Reference Related Methods" .PP Besides the \*(L"Functions for SCALARs\*(R" section of perlfunc, the following were implemented, where they make sense: \&\f(CW\*(C`tie\*(C'\fR, \&\f(CW\*(C`tied\*(C'\fR, \&\f(CW\*(C`ref\*(C'\fR, \&\f(CW\*(C`undef\*(C'\fR, \&\f(CW\*(C`bless\*(C'\fR, and \f(CW\*(C`vec\*(C'\fR. \&\f(CW\*(C`tie\*(C'\fR, \f(CW\*(C`tied\*(C'\fR, and \f(CW\*(C`undef\*(C'\fR don't work on code references, and \f(CW\*(C`bless\*(C'\fR doesn't work on non-reference scalars (okay, that's no longer true). \&\f(CW\*(C`quotemeta\*(C'\fR works on non-reference scalars, along with \f(CW\*(C`split\*(C'\fR, \f(CW\*(C`m\*(C'\fR, and \f(CW\*(C`s\*(C'\fR for regular expression operations. \&\f(CW\*(C`ref\*(C'\fR is the same as the \f(CW\*(C`ref\*(C'\fR keyword in that it tells you what kind of a reference something is if it's a reference; \s-1XXX\s0 there's currently no counterpart to the \f(CW\*(C`\e\*(C'\fR operator, which takes something and gives you a reference to it. .PP \fIArray Methods\fR .IX Subsection "Array Methods" .PP Array methods work on both arrays and array references: .PP .Vb 2 \& my $arr = [ 1 .. 10 ]; \& $arr\->undef; .Ve .PP Or: .PP .Vb 2 \& my @arr = [ 1 .. 10 ]; \& @arr\->undef; .Ve .PP Arrays can tell you how many elements they contain and the index of their last element: .PP .Vb 3 \& my $arr = [ 1 .. 10 ]; \& print \*(Aq$arr contains \*(Aq, $arr\->size, \& \*(Aq elements, the last having an index of \*(Aq, $arr\->last_index, "\en"; .Ve .PP Array references have a \f(CW\*(C`flatten\*(C'\fR method to dump their elements. This is the same as \f(CW\*(C`@{$array_ref}\*(C'\fR. .PP .Vb 2 \& my $arr = [ 1 .. 10 ]; \& print join \*(Aq \-\- \*(Aq, $arr\->flatten, "\en"; .Ve .PP List context forces methods to return a list: .PP .Vb 2 \& my @arr = ( 1 .. 10 ); \& print join \*(Aq \-\- \*(Aq, @arr\->grep(sub { $_ > 3 }), "\en"; .Ve .PP Methods may be chained; scalar context forces methods to return a reference: .PP .Vb 2 \& my @arr = ( 1 .. 10 ); \& print @arr\->grep(sub { $_ > 3 })\->min, "\en"; .Ve .PP Arrays can be iterated on using \f(CW\*(C`for\*(C'\fR and \f(CW\*(C`foreach\*(C'\fR. Both take a code reference as the body of the for statement. \&\f(CW\*(C`foreach\*(C'\fR passes the current element itself in each pass. \&\f(CW\*(C`for\*(C'\fR 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. .PP .Vb 3 \& my $arr = [ 1 .. 10 ]; \& $arr\->foreach(sub { print $_[0], "\en" }); \& $arr\->for(sub { die unless $_[1] == $_[2]\->[$_[0]] }); .Ve .PP \&\f(CW\*(C`sum\*(C'\fR is a toy poke at doing Language::Functional\-like stuff: .PP .Vb 1 \& print $arrref\->sum, "\en"; .Ve .PP Methods for array creation: \f(CW\*(C`to\*(C'\fR, \f(CW\*(C`upto\*(C'\fR, and \f(CW\*(C`downto\*(C'\fR. .PP .Vb 3 \& 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] .Ve .PP These wrap the \f(CW\*(C`..\*(C'\fR operator. .PP .Vb 1 \& $arr\->first(sub { /5/ }); .Ve .PP head .IX Subsection "head" .PP .Vb 1 \& my $first = @list\->head; .Ve .PP Returns the first element from \f(CW@list\fR. .PP tail .IX Subsection "tail" .PP .Vb 2 \& my @list = qw(foo bar baz quux); \& my @rest = @list\->tail; # [ \*(Aqbar\*(Aq, \*(Aqbaz\*(Aq, \*(Aqquux\*(Aq ] .Ve .PP Returns all but the first element from \f(CW@list\fR. In scalar context returns an array reference. .PP Optionally, you can pass a number as argument to ask for the last \f(CW$n\fR elements: .PP .Vb 1 \& @rest = @list\->tail(2); # [ \*(Aqbaz\*(Aq, \*(Aqquux\*(Aq ] .Ve .PP slice .IX Subsection "slice" .PP .Vb 1 \& my @sublist = @list\->slice(@indexes); .Ve .PP Returns a list containing the elements from \f(CW@list\fR at the indices \&\f(CW@indices\fR. In scalar context, returns an array reference. .PP range .IX Subsection "range" .PP .Vb 1 \& my @sublist = @list\->range( $lower_idx, $upper_idx ); .Ve .PP Returns a list containing the elements from \f(CW@list\fR with indices ranging from \f(CW$lower_idx\fR to \f(CW$upper_idx\fR. Returns an array reference in scalar context. .PP last_index .IX Subsection "last_index" .PP .Vb 1 \& my $last_index = @array\->last_index .Ve .PP Returns \f(CW@array\fR's last index. Optionally, takes a Coderef or a Regexp, in which case it will return the index of the last element that matches such regex or makes the code reference return true. Example: .PP .Vb 1 \& my @things = qw(pear poll potato tomato); \& \& my $last_p = @things\->last_index(qr/^p/); # 2 .Ve .PP first_index .IX Subsection "first_index" .PP .Vb 1 \& my $first_index = @array\->first_index; # 0 .Ve .PP For simmetry, returns the first index of \f(CW@array\fR. If passed a Coderef or Regexp, it will return the index of the first element that matches. .PP .Vb 1 \& my @things = qw(pear poll potato tomato); \& \& my $last_p = @things\->first_index(qr/^t/); # 3 .Ve .PP \fIHash Methods\fR .IX Subsection "Hash Methods" .PP \&\f(CW\*(C`each\*(C'\fR is like \f(CW\*(C`foreach\*(C'\fR but for hash references. For each key in the hash, the code reference is invoked with the key and the corresponding value as arguments: .PP .Vb 2 \& my $hashref = { foo => 10, bar => 20, baz => 30, quux => 40 }; \& $hashref\->each(sub { print $_[0], \*(Aq is \*(Aq, $_[1], "\en" }); .Ve .PP 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. .PP \&\f(CW\*(C`slice\*(C'\fR takes a list of hash keys and returns the corresponding values e.g. .PP .Vb 5 \& my %hash = ( \& one => \*(Aqtwo\*(Aq, \& three => \*(Aqfour\*(Aq, \& five => \*(Aqsix\*(Aq \& ); \& \& print %hash\->slice(qw(one five))\->join(\*(Aq and \*(Aq); # prints "two and six" .Ve .PP \fIflip()\fR .IX Subsection "flip()" .PP Exchanges values for keys in a hash. .PP .Vb 2 \& my %things = ( foo => 1, bar => 2, baz => 5 ); \& my %flipped = %things\->flip; # { 1 => foo, 2 => bar, 5 => baz } .Ve .PP If there is more than one occurence of a certain value, any one of the keys may end up as the value. This is because of the random ordering of hash keys. .PP .Vb 2 \& # Could be { 1 => foo }, { 1 => bar }, or { 1 => baz } \& { foo => 1, bar => 1, baz => 1 }\->flip; .Ve .PP Because hash references cannot usefully be keys, it will not work on nested hashes. .PP .Vb 1 \& { foo => [ \*(Aqbar\*(Aq, \*(Aqbaz\*(Aq ] }\->flip; # dies .Ve .PP \fICode Methods\fR .IX Subsection "Code Methods" .PP You may \f(CW\*(C`curry\*(C'\fR code references: .PP .Vb 5 \& $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; "\en"\->print; .Ve .PP \&\f(CW\*(C`times\*(C'\fR executes a coderef a given number of times: .PP .Vb 1 \& 5\->times(sub { print "hi\en"}); # XXX likely to change but it\*(Aqs in the code so bloody doc it so I have incentive to rethink it .Ve .PP \&\s-1XXX\s0 round this out .SS "What's Missing?" .IX Subsection "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. .PP File and socket operations are already implemented in an object-oriented fashion care of IO::Handle, IO::Socket::INET, and IO::Any. .PP Functions listed in the perlfunc headings \*(L"System V interprocess communication functions\*(R", \&\*(L"Fetching user and group info\*(R", \&\*(L"Fetching network info\*(R", \&\*(L"Keywords related to perl modules\*(R", \&\*(L"Functions for processes and process groups\*(R", \&\*(L"Keywords related to scoping\*(R", \&\*(L"Time-related functions\*(R", \&\*(L"Keywords related to the control flow of your perl program\*(R", \&\*(L"Functions for filehandles, files, or directories\*(R", and \&\*(L"Input and output functions\*(R". These things are likely implemented in an object oriented fashion by other \s-1CPAN\s0 modules, are keywords and not functions, take no arguments, or don't make sense as part of the string, number, array, hash, or code \s-1API\s0. \&\f(CW\*(C`srand\*(C'\fR because you probably shouldn't be using it. .PP \&\f(CW\*(C`each\*(C'\fR on hashes. There is no good reason it is missing. \s-1XXX\s0. .SS "Autoboxing" .IX Subsection "Autoboxing" \&\fIThis section quotes four pages from the manuscript of Perl 6 Now: The Core Ideas Illustrated with Perl 5 by myself, Scott Walters. The text appears in the book starting at page 248. This copy lacks the benefit of copyedit \- the finished product is of higher quality. See the shameless plug in the \s-1SEE\s0 \s-1ALSO\s0 section for information on ordering Perl 6 Now.\fR .PP A \fIbox\fR is an object that contains a primitive variable. Boxes are used to endow primitive types with the capabilities of objects. This is essential in strongly typed languages but never strictly required in Perl. Programmers might write something like \f(CW\*(C`my $number = Int\->new(5)\*(C'\fR. This is manual boxing. To \fIautobox\fR is to convert a simple type into an object type automatically, or only conceptually. This is done by the language. It makes a language look to programmers as if everything is an object while the interpreter is free to implement data storage however it pleases. Autoboxing is really making simple types such as numbers, strings, and arrays appear to be objects. .PP \&\f(CW\*(C`int\*(C'\fR, \f(CW\*(C`num\*(C'\fR, \f(CW\*(C`bit\*(C'\fR, \f(CW\*(C`str\*(C'\fR, and other types with lower case names, are primitives. They're fast to operate on, and require no more memory to store than the data held strictly requires. \&\f(CW\*(C`Int\*(C'\fR, \f(CW\*(C`Num\*(C'\fR, \f(CW\*(C`Bit\*(C'\fR, \f(CW\*(C`Str\*(C'\fR, and other types with an initial capital letter, are objects. These may be subclassed (inherited from) and accept traits, among other things. These objects are provided by the system for the sole purpose of representing primitive types as objects, though this has many ancillary benefits such as making \f(CW\*(C`is\*(C'\fR and \f(CW\*(C`has\*(C'\fR work. Perl provides \f(CW\*(C`Int\*(C'\fR to encapsulate an \f(CW\*(C`int\*(C'\fR, \f(CW\*(C`Num\*(C'\fR to encapsulate a \f(CW\*(C`num\*(C'\fR, \f(CW\*(C`Bit\*(C'\fR to encapsulate a \f(CW\*(C`bit\*(C'\fR, and so on. As Perl's implementations of hashes and dynamically expandable arrays store any type, not just objects, Perl programmers almost never are required to box primitive types in objects. Perl's power makes this feature less essential than it is in other languages. .PP ing makes primitive objects and they're boxed versions equivalent. An \f(CW\*(C`int\*(C'\fR may be used as an \f(CW\*(C`Int\*(C'\fR with no constructor call, no passing, nothing. This applies to constants too, not just variables: .IX Xref "autobox" .PP .Vb 1 \& # Perl 6 \- autoboxing associates classes with primitives types: \& \& print 4.sqrt, "\en"; .Ve .PP This is perfectly valid Perl 6. .PP All of this applies to hashes and arrays, as well: .PP .Vb 1 \& # Perl 6 \- autoboxing associates classes with primitive types: \& \& print [ 1 .. 20 ].elems, "\en"; .Ve .PP The language is free to implement data storage however it wishes but the programmer sees the variables as objects. .PP Expressions using autoboxing read somewhat like Latin suffixes. In the autoboxing mind-set, you might not say that something is \*(L"made more mnemonic\*(R", but has been \*(L"mnemonicified\*(R". .PP Autoboxing may be mixed with normal function calls. In the case where the methods are available as functions and the functions are available as methods, it is only a matter of personal taste how the expression should be written: .PP .Vb 2 \& # Calling methods on numbers and strings, these three lines are equivalent \& # Perl 6 \& \& print sqrt 4; \& print 4.sqrt; \& 4.sqrt.print; .Ve .PP The first of these three equivalents assumes that a global \f(CW\*(C`sqrt()\*(C'\fR function exists. This first example would fail to operate if this global function were removed and only a method in the \f(CW\*(C`Num\*(C'\fR package was left. .PP Perl 5 had the beginnings of autoboxing with filehandles: .PP .Vb 3 \& use IO::Handle; \& open my $file, \*(Aq<\*(Aq, \*(Aqfile.txt\*(Aq or die $!; \& $file\->read(my $data, \-s $file); .Ve .PP Here, \f(CW\*(C`read\*(C'\fR is a method on a filehandle we opened but \fInever blessed\fR. This lets us say things like \f(CW\*(C`$file\->print(...)\*(C'\fR rather than the often ambagious .PP \&\f(CW\*(C`print $file ...\*(C'\fR. To many people, much of the time, it makes more conceptual sense as well. .PP \fIReasons to Box Primitive Types\fR .IX Subsection "Reasons to Box Primitive Types" .PP What good is all of this? .IP "Makes conceptual sense to programmers used to object interfaces as \fIthe\fR way to perform options." 1 .IX Item "Makes conceptual sense to programmers used to object interfaces as the way to perform options." .PD 0 .IP "Alternative idiom. Doesn't require the programmer to write or read expressions with complex precedence rules or strange operators." 1 .IX Item "Alternative idiom. Doesn't require the programmer to write or read expressions with complex precedence rules or strange operators." .IP "Many times that parenthesis would otherwise have to span a large expression, the expression may be rewritten such that the parenthesis span only a few primitive types." 1 .IX Item "Many times that parenthesis would otherwise have to span a large expression, the expression may be rewritten such that the parenthesis span only a few primitive types." .IP "Code may often be written with fewer temporary variables." 1 .IX Item "Code may often be written with fewer temporary variables." .ie n .IP "Autoboxing provides the benefits of boxed types without the memory bloat of actually using objects to represent primitives. Autoboxing ""fakes it""." 1 .el .IP "Autoboxing provides the benefits of boxed types without the memory bloat of actually using objects to represent primitives. Autoboxing ``fakes it''." 1 .IX Item "Autoboxing provides the benefits of boxed types without the memory bloat of actually using objects to represent primitives. Autoboxing fakes it." .ie n .IP "Strings, numbers, arrays, hashes, and so on, each have their own \s-1API\s0. Documentation for an ""exists"" method for arrays doesn't have to explain how hashes are handled and vice versa." 1 .el .IP "Strings, numbers, arrays, hashes, and so on, each have their own \s-1API\s0. Documentation for an \f(CWexists\fR method for arrays doesn't have to explain how hashes are handled and vice versa." 1 .IX Item "Strings, numbers, arrays, hashes, and so on, each have their own API. Documentation for an exists method for arrays doesn't have to explain how hashes are handled and vice versa." .ie n .IP "Perl tries to accommodate the notion that the ""subject"" of a statement should be the first thing on the line, and autoboxing furthers this agenda." 1 .el .IP "Perl tries to accommodate the notion that the ``subject'' of a statement should be the first thing on the line, and autoboxing furthers this agenda." 1 .IX Item "Perl tries to accommodate the notion that the subject of a statement should be the first thing on the line, and autoboxing furthers this agenda." .PD .PP Perl is an idiomatic language and this is an important idiom. .PP \fISubject First: An Aside\fR .IX Subsection "Subject First: An Aside" .PP Perl's design philosophy promotes the idea that the language should be flexible enough to allow programmers to place the of a statement first. For example, \f(CW\*(C`die $! unless read $file, 60\*(C'\fR looks like the primary purpose of the statement is to \f(CW\*(C`die\*(C'\fR. While that might be the programmers primary goal, when it isn't, the programmer can communicate his real primary intention to programmers by reversing the order of clauses while keeping the exact same logic: \f(CW\*(C`read $file, 60 or die $!\*(C'\fR. Autoboxing is another way of putting the subject first. Nouns make good subjects, and in programming, variables, constants, and object names are the nouns. Function and method names are verbs. \&\f(CW\*(C`$noun\->verb()\*(C'\fR focuses the readers attention on the thing being acted on rather than the action being performed. Compare to \f(CW\*(C`$verb($noun)\*(C'\fR. .IX Xref "subject" .PP \fIAutoboxing and Method Results\fR .IX Subsection "Autoboxing and Method Results" .PP In Chapter 11 [Subroutines], we had examples of ways an expression could be written. Here it is again: .PP .Vb 1 \& # Various ways to do the same thing: \& \& print(reverse(sort(keys(%hash)))); # Perl 5 \- pathological parenthetic \& print reverse sort keys %hash; # Perl 5 \- no unneeded parenthesis \& \& print(reverse(sort(%hash,keys)))); # Perl 6 \- pathological \& print reverse sort %hash.keys; # Perl 6 \- no unneeded parenthesis \& \& %hash.keys ==> sort ==> reverse ==> print; # Perl 6 \- pipeline operator \& \& %hash.keys.sort.reverse.print; # Perl 6 \- autobox \& \& %hash\->keys\->sort\->reverse\->print; # Perl 5 \- autobox .Ve .PP This section deals with the last two of these equivalents. These are method calls use autobox::Core; use Perl6::Contexts; .PP .Vb 1 \& my %hash = (foo => \*(Aqbar\*(Aq, baz => \*(Aqquux\*(Aq); \& \& %hash\->keys\->sort\->reverse\->print; # Perl 5 \- autobox \& \& # prints "foo baz" .Ve .PP Each method call returns an array reference, in this example. Another method call is immediately performed on this value. This feeding of the next method call with the result of the previous call is the common mode of use of autoboxing. Providing no other arguments to the method calls, however, is not common. .PP \&\fIPerl6::Contexts\fR recognizes object context as provided by \f(CW\*(C`\->\*(C'\fR and coerces \f(CW%hash\fR and \f(CW@array\fR into references, suitable for use with \fIautobox\fR. (Note that \fIautobox\fR also does this automatically as of version 2.40.) \&\fIautobox\fR associates primitive types, such as references of various sorts, with classes. \&\fIautobox::Core\fR throws into those classes methods wrapping Perl's built-in functions. In the interest of full disclosure, \fIPerl6::Contexts\fR and \fIautobox::Core\fR are my creations. .PP \fIAutobox to Simplify Expressions\fR .IX Subsection "Autobox to Simplify Expressions" .PP One of my pet peeves in programming is parenthesis that span large expression. It seems like about the time I'm getting ready to close the parenthesis I opened on the other side of the line, I realize that I've forgotten something, and I have to arrow back over or grab the mouse. When the expression is too long to fit on a single line, it gets broken up, then I must decide how to indent it if it grows to 3 or more lines. .PP .Vb 1 \& # Perl 5 \- a somewhat complex expression \& \& print join("\en", map { CGI::param($_) } @cgi_vars), "\en"; \& # Perl 5 \- again, using autobox: \& \& @cgi_vars\->map(sub { CGI::param($_[0]) })\->join("\en")\->concat("\en")\->print; .Ve .PP The autoboxed version isn't shorter, but it reads from left to right, and the parenthesis from the \f(CW\*(C`join()\*(C'\fR don't span nearly as many characters. The complex expression serving as the value being \f(CW\*(C`join()\*(C'\fRed in the non-autoboxed version becomes, in the autoboxed version, a value to call the \f(CW\*(C`join()\*(C'\fR method on. .PP This \f(CW\*(C`print\*(C'\fR statement takes a list of \s-1CGI\s0 parameter names, reads the values for each parameter, joins them together with newlines, and prints them with a newline after the last one. .PP Pretending that this expression were much larger and it had to be broken to span several lines, or pretending that comments are to be placed after each part of the expression, you might reformat it as such: .PP .Vb 4 \& @cgi_vars\->map(sub { CGI::param($_[0]) }) # turn CGI arg names into values \& \->join("\en") # join with newlines \& \->concat("\en") # give it a trailing newline \& \->print; # print them all out .Ve .PP This could also have been written: .PP .Vb 4 \& sub { CGI::param($_[0]) }\->map(@cgi_vars) # turn CGI arg names into values \& \->join("\en") # join with newlines \& \->concat("\en") # give it a trailing newline \& \->print; # print them all out .Ve .PP \&\f(CW\*(C`map()\*(C'\fR is . The \f(CW\*(C`map()\*(C'\fR method defined in the \f(CW\*(C`autobox::Core::CODE\*(C'\fR package takes for its arguments the things to map. The \f(CW\*(C`map()\*(C'\fR method defined in the \f(CW\*(C`autobox::Core::ARRAY\*(C'\fR package takes for its argument a code reference to apply to each element of the array. .IX Xref "polymorphic" .PP \&\fIHere ends the text quoted from the Perl 6 Now manuscript.\fR .SH "BUGS" .IX Header "BUGS" Yes. Report them to the author, scott@slowass.net. The \s-1API\s0 is not yet stable \*(-- Perl 6\-ish things and local extensions are still being renamed. .SH "HISTORY" .IX Header "HISTORY" See the Changes file. .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright (C) 2009, 2010 by Scott Walters and various contributors listed (and unlisted) below .PP 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. .PP This library is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. .SH "SEE ALSO" .IX Header "SEE ALSO" .IP "autobox" 1 .IX Item "autobox" .PD 0 .IP "Moose::Autobox" 1 .IX Item "Moose::Autobox" .IP "Perl6::Contexts" 1 .IX Item "Perl6::Contexts" .IP "http://github.com/gitpan/autobox\-Core " 1 .IX Item "http://github.com/gitpan/autobox-Core " .IP "IO::Any" 1 .IX Item "IO::Any" .IP "Perl 6: ." 1 .IX Item "Perl 6: ." .IP "(Shameless plug:) \fIPerl 6 Now: The Core Ideas Illustrated with Perl 5\fR dedicates a sizable portion of Chapter 14, Objects, to autoboxing and the idea is used heavily throughout the book. Chapter 8, Data Structures, also has numerous examples. See or look for \s-1ISBN\s0 1\-59059\-395\-2 at your favorite bookstore for more information." 1 .IX Item "(Shameless plug:) Perl 6 Now: The Core Ideas Illustrated with Perl 5 dedicates a sizable portion of Chapter 14, Objects, to autoboxing and the idea is used heavily throughout the book. Chapter 8, Data Structures, also has numerous examples. See or look for ISBN 1-59059-395-2 at your favorite bookstore for more information." .PD .SH "AUTHORS" .IX Header "AUTHORS" Scott Walters, scott@slowass.net. .PP Michael Schwern and the perl5i contributors for tests, code, and feedback. .PP \&\s-1JJ\s0 contributed a \f(CW\*(C`strip\*(C'\fR method for scalars \- thanks \s-1JJ\s0! .PP Ricardo \s-1SIGNES\s0 contributed patches. .PP Thanks to Matt Spear, who contributed tests and definitions for numeric operations. .PP Mitchell N Charity reported a bug and sent a fix. .PP Thanks to chocolateboy for autobox and for the encouragement. .PP Thanks to Bruno Vecchi for bug fixes and many, many new tests going into version 0.8. .PP Thanks to daxim/Lars \s-1DIECKOW\s0 pushing in fixes and patches from the \s-1RT\s0 queue along with fixes to build and additional doc examples.