NAME

autobox - use builtin data types as first-class objects

SYNOPSIS

    use autobox;

    # call methods on builtin values and literals

    # integers

	my $range = 10->to(1); # [ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ]

    # floats

	my $error = 3.1415927->minus(22/7)->abs();

    # strings

	my $uri = 'www.%s.com/foo.pl?arg=%s'->f($domain, $arg->escape());
	my $links = 'autobox'->google();

	my $word = 'rubicund';
	my $definition = $word->lookup_on_dictionary_dot_com();

	my $greeting = "Hello, World"->upper(); # "HELLO, WORLD"

	$greeting->to_lower(); # greeting is now "hello, world"
	$greeting->for_each(\&character_handler);

    # ARRAY refs

	my $schwartzian = [ @_ ]->map(...)->sort(...)->map(...);
	my $sd = [ 1, 8, 3, 3, 2, 9 ]->standard_deviation();

    # HASH refs

	{ alpha => 'beta', gamma => 'vlissides' }->for_each(...);

    # CODE refs

	my $plus_five = (\&add)->curry()->(5);
	my $minus_three = sub { $_[0] - $_[1] }->reverse->curry->(3);

    # can() and isa() work as expected

	if ("Hello, World"->can('foo')) ...
	if (3.1415927->isa('SCALAR')) ...

DESCRIPTION

The autobox pragma endows Perl's core data types with the capabilities of first-class objects. This allows methods to be called on ARRAY refs, HASH refs, CODE refs and raw scalars in exactly the same manner as blessed references. The autoboxing is transparent: boxed values are not blessed into their (user-defined) implementation class (unless the method elects to bestow such a blessing) - they simply use its methods as though they are.

The classes (packages) into which the core types are boxed are fully configurable. By default, a method invoked on a non-object value is assumed to be defined in a class whose name corresponds to the ref() type of that value - or SCALAR if the value is a non-reference.

This mapping can be overriden by passing key/value pairs to the use autobox statement, in which the keys represent builtin types, and the values their associated classes.

As with regular objects, autoboxed values are passed as the first argument of the specified method. Consequently, given a vanilla use autobox:

"hello, world"->upper()

is invoked as:

SCALAR::upper("hello, world")

while:

[ 1 .. 10 ]->for_each(sub { ... })

resolves to:

ARRAY::for_each([ 1 .. 10 ], sub { ... })

Multiple use autobox statements can appear in the same scope. These are merged both "horizontally" (i.e. mutiple classes can be associated with a particular builtin type) and "vertically" (i.e. a previously unboxed type can be associated with a class).

Thus:

use autobox SCALAR	=> 'Foo';
use autobox SCALAR	=> 'Bar';

- associates SCALAR types with a synthetic class whose @ISA includes both Foo and Bar (in that order).

Likewise:

use autobox SCALAR	=> 'Foo';
use autobox ARRAY	=> 'Bar';

- binds SCALAR types to the Foo class and ARRAY types to Bar.

autobox is lexically scoped, and handlers (see below) for an outer scope can be overridden or countermanded in a nested scope:

    {
	use autobox; # default handlers: autobox all builtins
	...

	{
	    use autobox SCALAR => 'MyScalar'; # appends 'MyScalar' to the @ISA associated with SCALAR types
	    ...
	}

	# back to the default (no MyScalar)
	...
    }

Autoboxing can be turned off entirely by using the no syntax:

    {
	use autobox;
	...
	no autobox;
	...
    }

- or can be selectively reset or disabled by passing arguments to the no autobox statement:

use autobox; # default bindings

no autobox qw(SCALAR);

[]->foo(); # OK: ARRAY::foo([])

"hello"->bar(); # runtime error

Autoboxing is not performed for barewords i.e.

my $foo = Foo->new();

and:

my $foo = new Foo;

behave as expected.

In addition, it only covers named methods, so while this works:

my $foobar = { foo => 'bar' }->some_method();

These don't:

my $method1 = 'some_method';
my $method2 = \&HASH::some_method;

my $error1 = { foo => 'bar' }->$method1();
my $error2 = { foo => 'bar' }->$method2();

A builtin type is only asociated with a class if the type => class mapping is supplied in the use autobox statement. Thus the following will not work:

use autobox SCALAR => 'MyScalar';

[]->some_array_method();

- as no class is specified for the ARRAY type. Note: the result of calling a method on a number, string or unblessed reference that is not associated with a class is the usual runtime error message:

Can't call method "some_array_method" on unblessed reference at ...

As a convenience, there is one exception to this rule. If use autobox is invoked with no arguments (ignoring the DEBUG option) the four main builtin types are associated with classes of the same name.

Thus:

use autobox;

- is equivalent to:

    use autobox
	SCALAR	=> 'SCALAR',
	ARRAY	=> 'ARRAY',
	HASH	=> 'HASH',
	CODE	=> 'CODE';

This facilitates one-liners and prototypes:

use autobox;

sub SCALAR::split { [ split '', $_[0] ] }
sub ARRAY::length { scalar @{$_[0]} }

print "Hello, world!"->split->length();

However, using these builtin bindings is not recommended as there's no guarantee that another piece of code won't trample over the same namespace/methods.

A mapping from the builtin type to the user-defined class can be specified by passing a list of key/value pairs to the use autobox statement.

The following example shows the range of valid arguments:

    use autobox SCALAR  => 'MyScalar'				# class name
		ARRAY   => 'MyNamespace::',			# class prefix (ending in '::')
		HASH	=> [ 'MyScalar', 'MyNamespace::' ],	# one or more class names and/or prefixes
		CODE    => ...,					# any of the 3 value types above
		UNDEF   => ...,					# any of the 3 value types above
		DEFAULT => ...,					# any of the 3 value types above
		DEBUG   => ...;					# boolean or coderef

The SCALAR, ARRAY, HASH, CODE, UNDEF and DEFAULT keys can take three different types of value:

  • A class name e.g.

    use autobox SCALAR => 'MyScalar';

    This binds the specified builtin type to the specified class. All methods invoked on literals or values of builtin type 'key' will be dispatched as methods of the class specified in the corresponding 'value'.

    If a class name is supplied for DEFAULT, it becomes the default class for all unspecified cases for that invocation of use autobox. Thus:

        use autobox ARRAY	=> 'MyArray',
    		DEFAULT => 'MyDefault';

    - will invoke ARRAY methods on MyArray and all other methods on MyDefault.

  • A namespace: this is a class prefix (up to and including the final '::') to which the specified builtin type name (SCALAR, ARRAY, HASH or CODE) will be appended:

    Thus:

    use autobox ARRAY => 'Prelude::';

    binds ARRAY types to the Prelude::ARRAY class.

    As with the class name form, specifying a default namespace e.g.

        use autobox SCALAR	=> 'MyScalar',
    		DEFAULT => 'MyNamespace::';

    binds MyNamespace::ARRAY, MyNamespace::HASH &c. to the corresponding builtin types.

  • A reference to an array of class names and/or namespaces. This associates multiple classes with the specified builtin type.

In addition to the SCALAR, ARRAY, HASH, CODE and DEFAULT options, there are two additional keys: UNDEF and DEBUG.

UNDEF

The pseudotype, UNDEF, can be used to autobox undefined values. These are not autoboxed by default.

This doesn't work:

use autobox;

undef->foo() # runtime error

This works:

use autobox UNDEF => 'MyClass'; 

undef->foo(); # ok

So does this:

use autobox UNDEF => 'MyNamespace::'; 

undef->foo(); # ok

DEBUG

DEBUG exposes the current handlers for the scope in which use autobox is called by means of a callback, or a static debugging function.

This allows the computed bindings to be seen in 'longhand'.

Debugging is ignored if the value corresponding to the DEBUG key is false.

If the value is a CODE ref, then this sub is called with a reference to the hash containing the computed handlers for the current scope.

Finally, if DEBUG is true but not a CODE ref, the handlers are dumped to STDERR.

Thus:

use autobox DEBUG => 1, ...

or

use autobox DEBUG => sub { ... }, ...

or

    sub my_callback ($) {
	my $hashref = shift;
	...
    }

    use autobox DEBUG => \&my_callback, ...

METHODS

On its own, autobox doesn't implement any methods that can be called on builtin types (apart from can and isa). However, it does implement two methods that subclasses can override to provide autobox extensions i.e. bundles of methods for one or more builtin types. These can be composed both "horizontally" (multiple modules for the same type) and "vertically" (multiple modules for different types).

import

The simplest way to implement an autobox extension is to inherit from autobox and override import. This allows subclasses to effectively translate use MyModule into a bespoke use autobox call. e.g.:

    package String::Trim;

    use base qw(autobox);

    sub import {
	my $class = shift;
	$class->SUPER::import(SCALAR => 'String::Trim::Scalar');
    }

    package String::Trim::Scalar;

    sub trim {
	my $string = shift;
	$string =~ s/^\s+//;
	$string =~ s/\s+$//;
	$string;
    }

    1;

Note that trim is defined in an auxilliary class rather than in String::Trim itself to prevent String::Trim's own methods (i.e. the methods it inherits from autobox) being exposed to SCALAR types.

Clients can now use this module to enable the trim method in the current lexical scope. e.g.:

use String::Trim;

print "  Hello, world!  "->trim();

defaults

For more extensive wrappers, autobox implements a defaults method that returns a HASH ref that defines the module's default bindings.

    package Prelude;

    use base qw(autobox);

    sub defaults {
	my $class = shift;

	return {
	    SCALAR	=> 'Foo::Scalar',
	    ARRAY	=> 'Bar::Array',
	    HASH	=> [ qw(Baz::Hash Quux::Hash) ],
	    UNDEF	=> undef,
	    DEFAULT	=> undef
	};
    }

    1;

The HASH ref returned by the defaults method serves three purposes: 1) its keys constrain the valid keys that can be supplied to the use Prelude statement (in the above example, use Prelude CODE = 'MyCode'> triggers a compile-time error as CODE is not is not included in the default keys); 2) it can define a DEFAULT binding for all undefined types; and 3) it can supply default bindings when use Prelude is called with no arguments e.g.:

use Prelude;

# equivalent to use autobox SCALAR => 'Foo::Scalar', ARRAY => 'Bar::Array' &c.

Modules without options can simply ignore arguments passed in the use statement to provide full control over autobox's bindings:

    package NoArgs;

    sub defaults { ... }

    sub import {
	my $class = shift;
	my $defaults = $class->defaults;
	$class->SUPER::import(%$defaults);
    }

    1;

Clients of NoArgs cannot customize or override its bindings, although, of course, other invocations of use autobox or the use of other subclasses of autobox in the same or nested scopes can.

The defaults method can also be used to enable autoboxing for new types such as LVALUEs, GLOBs, or FORMATs.

CAVEATS

PERFORMANCE

Autoboxing comes at a price. Calling

"Hello, world!"->length()

is slightly slower than the equivalent method call on a string-like class, and significantly slower than

length("Hello, world!")

GOTCHAS

Due to Perl's precedence rules, some autoboxed literals may need to be parenthesized:

For instance, while this works:

my $curried = sub { ... }->curry();

this doesn't:

my $curried = \&foo->curry();

The solution is to wrap the reference in parentheses:

my $curried = (\&foo)->curry();

The same applies for signed integer and float literals:

# this works
my $range = 10->to(1);

# this doesn't work
my $range = -10->to(10);

# this works
my $range = (-10)->to(10);

Perl's special-casing for the print BLOCK ... syntax (see perlsub) means that print { expression() } ... (where the curly brackets denote an anonymous HASH ref) may require some further disambiguation:

# this works (
print { foo => 'bar' }->foo();

# and this
print { 'foo', 'bar' }->foo();

# and even this
print { 'foo', 'bar', @_ }->foo();

# but this doesn't
print { @_ }->foo() ? 1 : 0 

In the latter case, the solution is to supply something other than a HASH ref literal as the first argument to print():

# e.g.
print STDOUT { @_ }->foo() ? 1 : 0;

# or
my $hashref = { @_ };
print $hashref->foo() ? 1 : 0; 

# or
print '', { @_ }->foo() ? 1 : 0; 

# or
print '' . { @_ }->foo() ? 1 : 0; 

# or even
{ @_ }->print_if_foo(1, 0); 

Although can and isa are "overloaded" for autoboxed values, the VERSION method isn't. Thus, while these work:

[ ... ]->can('pop')

3.1415->isa('MyScalar')

This doesn't:

use MyScalar 1.23;

use autobox SCALAR => MyScalar;

print "Hello, World"->VERSION(), $/;

Though, of course:

print MyScalar->VERSION(), $/;

and

print $MyScalar::VERSION, $/;

continue to work.

This is due to a limitation in perl's implementation of use and no. Likewise, import and unimport are unaffected by the autobox pragma:

    'Foo'->import() # equivalent to Foo->import() rather than MyScalar->import('Foo')

    []->import()  # error: Can't call method "import" on unblessed reference
	

VERSION

2.01

SEE ALSO

AUTHOR

chocolateboy: <chocolate.boy@email.com>

COPYRIGHT

Copyright (c) 2003-2008, chocolateboy.

This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.