=head1 NAME autobox::Core - Perl built-in functions exposed as methods in primitive types =head1 SYNOPSIS use autobox; use autobox::Core; "Hello, World\n"->uc()->print(); =head1 DESCRIPTION Methods wrapping F'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. L 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 adds this feature to L but does not itself provide any methods to call. That is left to the user or another module. For example, this module. F is what you'd call a I module. It is merely glue, presenting existing functions with a new interface. Most of the methods read like C<< 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 C have been turned into methods. =head2 What's Implemented? All of the functions listed in L 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 C, C, and C, have been thrown in. For use in conjuction with L, C explicitly flattens an array. 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"; 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 L, I've implemented C, C, C, C, C, and C, where they make sense. C, C, and C don't work on code references, and C doesn't work on non-reference scalars. C works on non-reference scalars, along with C, C, and C for regular expression operations. 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 a C method to dump their elements. This is the same as C<< @{$array_ref} >>. my $arr = [ 1 .. 10 ]; print join " -- ", $arr->flatten, "\n"; Under L, 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 C and C. Both take a code reference as the body of the for statement. C passes the current element itself in each pass. C 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 = [ 1 .. 10 ]; $arr->foreach(sub { print $_[0], "\n" }); $arr->for(sub { die unless $_[1] == $_[2]->[$_[0]] }); C is a toy poke at doing L-like stuff: print $arrref->sum(), "\n"; If this goes over well, I'll make L a dependency and expose its function as methods on the correct data types. Or maybe I will do this anyway. C is like C 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 $_[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. C is C<< m// >> and C 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. C returns an array reference so that things such as C and C 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 is called on a non-reference scalar with the regular expression passed in. This is done for consistency with C and C. print "10, 20, 30, 40"->split(qr{, ?})->elements, "\n"; You may C 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. =head2 What's Missing? 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 L and L. Functions listed in the L 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. C because you probably shouldn't be using it. C on hashes. There is no good reason it is missing. =head2 Autoboxing I A I 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 C<< my $number = Int->new(5) >>. This is manual boxing. To I 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. C, C, C, C, 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. C, C, C, C, 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 C and C work. Perl provides C to encapsulate an C, C to encapsulate a C, C to encapsulate a C, 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. Xing makes primitive objects and they're boxed versions equivalent. An C may be used as an C with no constructor call, no passing, nothing. This applies to constants too, not just variables: # Perl 6 - autoboxing associates classes with primitives types: # Perl 6 - autobox 化はクラスとプリミティブ型を結びつけます. print 4.sqrt, "\n"; This is perfectly valid Perl 6. All of this applies to hashes and arrays, as well: # Perl 6 - autoboxing associates classes with primitive types: # Perl 6 - autobox 化はクラスとプリミティブ型を結びつけます. print [ 1 .. 20 ].elems, "\n"; The language is free to implement data storage however it wishes but the programmer sees the variables as objects. Expressions using autoboxing read somewhat like Latin suffixes. In the autoboxing mind-set, you might not say that something is "made more mnemonic", but has been "mnemonicified". 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: # Calling methods on numbers and strings, these three lines are equivalent # Perl 6 # 数字と文字に対してメソッドの呼び出し, この3行は Perl 6 的に等価です. print sqrt 4; print 4.sqrt; 4.sqrt.print; The first of these three equivalents assumes that a global C function exists. This first example would fail to operate if this global function were removed and only a method in the C package was left. Perl 5 had the beginnings of autoboxing with filehandles: use IO::Handle; open my $file, '<', 'file.txt' or die $!; $file->read(my $data, -s $file); Here, C is a method on a filehandle we opened but I. This lets us say things like C<< $file->print(...) >> rather than the often ambagious C<< print $file ... >>. To many people, much of the time, it makes more conceptual sense as well. =head3 Reasons to Box Primitive Types What good is all of this? =over 1 =item Makes conceptual sense to programmers used to object interfaces as I way to perform options. =item Alternative idiom. Doesn't require the programmer to write or read expressions with complex precedence rules or strange operators. =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. =item Code may often be written with fewer temporary variables. =item Autoboxing provides the benefits of boxed types without the memory bloat of actually using objects to represent primitives. Autoboxing "fakes it". =item Strings, numbers, arrays, hashes, and so on, each have their own API. Documentation for an C method for arrays doesn't have to explain how hashes are handled and vice versa. =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. =back Perl is an idiomatic language and this is an important idiom. =head3 Subject First: An Aside Perl's design philosophy promotes the idea that the language should be flexible enough to allow programmers to place the X of a statement first. For example, C<< die $! unless read $file, 60 >> looks like the primary purpose of the statement is to C. 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: C<< read $file, 60 or die $! >>. 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. C<< $noun->verb() >> focuses the readers attention on the thing being acted on rather than the action being performed. Compare to C<< $verb($noun) >>. =head3 Autoboxing and Method Results In Chapter 11 [Subroutines], we had examples of ways an expression could be written. Here it is again: # Various ways to do the same thing: # 同じことをするために様々な方法があります: print(reverse(sort(keys(%hash)))); # Perl 5 - pathological parenthetic # Perl 5 - 病的な括弧 print reverse sort keys %hash; # Perl 5 - no unneeded parenthesis # Perl 5 - 不要な括弧を削除 print(reverse(sort(%hash,keys)))); # Perl 6 - pathological # Perl 6 - 病的 print reverse sort %hash.keys; # Perl 6 - no unneeded parenthesis # Perl 6 - 不要な括弧を除去 %hash.keys ==> sort ==> reverse ==> print; # Perl 6 - pipeline operator # Perl 6 - パイプライン演算子 %hash.keys.sort.reverse.print; # Perl 6 - autobox %hash->keys->sort->reverse->print; # Perl 5 - autobox This section deals with the last two of these equivalents. These are method calls use autobox; use autobox::Core; use Perl6::Contexts; my %hash = (foo => 'bar', baz => 'quux'); %hash->keys->sort->reverse->print; # Perl 5 - autobox # prints "foo baz" 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. F recognizes object context as provided by C<< -> >> and coerces C<%hash> into a reference, suitable for use with F. F associates primitive types, such as references of various sorts, with classes. F throws into those classes methods wrapping Perl's built-in functions. In the interest of full disclosure, F and F are my creations. =head3 Autobox to Simplify Expressions 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. # Perl 5 - a somewhat complex expression # Perl 5 - 何か複雑な式 print join("\n", map { CGI::param($_) } @cgi_vars), "\n"; # Perl 5 - again, using autobox: # Perl 5 - もう一度, 今度は autobox で: @cgi_vars->map(sub { CGI::param($_[0]) })->join("\n")->concat("\n")->print; The autoboxed version isn't shorter, but it reads from left to right, and the parenthesis from the C don't span nearly as many characters. The complex expression serving as the value being Ced in the non-autoboxed version becomes, in the autoboxed version, a value to call the C method on. This C statement takes a list of CGI parameter names, reads the values for each parameter, joins them together with newlines, and prints them with a newline after the last one. 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: @cgi_vars->map(sub { CGI::param($_[0]) }) # turn CGI arg names into values # CGI 引数名を値に. ->join("\n") # join with newlines # 改行で連結. ->concat("\n") # give it a trailing newline # 末尾の改行を追加. ->print; # print them all out # それら全部を出力. This could also have been written: sub { CGI::param($_[0]) }->map(@cgi_vars) # turn CGI arg names into values # CGI 引数名を値に. ->join("\n") # join with newlines # 改行で連結. ->concat("\n") # give it a trailing newline # 末尾の改行を追加. ->print; # print them all out # それら全部を出力. C is X. The C method defined in the C package takes for its arguments the things to map. The C method defined in the C package takes for its argument a code reference to apply to each element of the array. I =head1 BUGS Yes. Report them to the author, L. This code is not well tested. The API is not yet stable - Perl 6-ish things and local extensions are still being renamed. =head1 HISTORY Version 0.3 fixes a problem where C wasn't sure it had enough arguments according to a test introduced in Perl 5.8.6 or perhaps 5.8.5. This problem was reported by Ron Reidy - thanks Ron! Version 0.3 also added the references to Perl 6 Now and the excerpt. Version 0.2 rounded out the API and introduced the beginnings of functional-ish methods. Version 0.1 was woefully incomplete. =head1 SEE ALSO L. L. Perl 6: L<< http://dev.perl.org/perl6/apocalypse/ >>. (Shameless plug alert!) I 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 L or look for ISBN 1-59059-395-2 at your favorite bookstore for more information. =head1 AUTHOR Scott Walters, L Thanks to chocolateboy for L and for the encouragement! =head1 TRANSALTE TO JAPANESE 山科 氷魚 (YAMASHINA Hio) Origlnal distribution is autobox-Core VERSION 0.3. Translated at 2006-10-26.