autobox-Core
view release on metacpan or search on metacpan
* 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",
* "Input and output functions".
* (Most) binary operators
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.
Autoboxing
*This section quotes four pages from the manuscript of Perl 6 Now: The
Core Ideas Illustrated with Perl 5 by 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.*
A *box* is an object that contains a primitive variable. Boxes are used
to endow primitive types with the capabilities of objects which
essential in strongly typed languages but never strictly required in
Perl. Programmers might write something like "my $number = Int->new(5)".
This is manual boxing. To *autobox* is to convert a simple type into an
object type automatically, or only conceptually. This is done by the
language.
*autobox*ing 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.
"int", "num", "bit", "str", 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. "Int", "Num", "Bit", "Str",
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 "is" and "has" work. Perl provides "Int" to
encapsulate an "int", "Num" to encapsulate a "num", "Bit" to encapsulate
a "bit", 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.
*autobox*ing makes primitive objects and they're boxed versions
equivalent. An "int" may be used as an "Int" with no constructor call,
no passing, nothing. This applies to constants too, not just variables.
This is a more Perl 6 way of doing things.
# Perl 6 - autoboxing associates classes with primitives types:
print 4.sqrt, "\n";
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
print sqrt 4;
print 4.sqrt;
4.sqrt.print;
The first of these three equivalents assumes that a global "sqrt()"
function exists. This first example would fail to operate if this global
function were removed and only a method in the "Num" 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, "read" is a method on a filehandle we opened but *never blessed*.
This lets us say things like "$file->print(...)" rather than the often
ambagious "print $file ...".
To many people, much of the time, it makes more conceptual sense as
well.
Reasons to Box Primitive Types
What good is all of this?
* Makes conceptual sense to programmers used to object interfaces as
*the* way to perform options.
* Alternative idiom. Doesn't require the programmer to write or read
expressions with complex precedence rules or strange operators.
* 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.
( run in 1.055 second using v1.01-cache-2.11-cpan-140bd7fdf52 )