autobox-Core

 view release on metacpan or  search on metacpan

lib/autobox/Core.pm  view on Meta::CPAN


=item *

"Time-related functions",

=item *

"Keywords related to the control flow of your perl program",

=item *

"Functions for filehandles, files, or directories",

=item *

"Input and output functions".

=back

=item *

(Most) binary operators

=back

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.

=head2 Autoboxing

I<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 I<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 C<< my $number = Int->new(5) >>.
This is manual boxing.  To I<autobox> is to convert a simple type into an
object type automatically, or only conceptually.  This is done by the language.

I<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.

C<int>, C<num>, C<bit>, C<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.  C<Int>, C<Num>, C<Bit>,
C<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 C<is> and C<has> work.  Perl provides C<Int> to
encapsulate an C<int>, C<Num> to encapsulate a C<num>, C<Bit> to
encapsulate a C<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.

I<autobox>ing makes primitive objects and they're boxed versions
equivalent.  An C<int> may be used as an C<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 C<sqrt()>
function exists.  This first example would fail to operate if this global
function were removed and only a method in the C<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, C<read> is a method on a filehandle we opened but I<never blessed>.
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 4

=item *

Makes conceptual sense to programmers used to object interfaces as I<the> way
to perform options.

=item *

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.799 second using v1.00-cache-2.02-grep-82fe00e-cpan-d29e8ade9f55 )