Name : perl-autobox
| |
Version : 3.0.2
| Vendor : obs://build_opensuse_org/devel:languages:perl
|
Release : 1.1
| Date : 2024-09-04 06:28:37
|
Group : Unspecified
| Source RPM : perl-autobox-3.0.2-1.1.src.rpm
|
Size : 0.08 MB
| |
Packager : (none)
| |
Summary : Call methods on native types
|
Description :
The autobox pragma allows methods to be called on integers, floats, strings, arrays, hashes, and code references in exactly the same manner as blessed references.
Autoboxing is transparent: 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 native 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 overridden by passing key/value pairs to the \'use autobox\' statement, in which the keys represent native 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 { ... })
Values beginning with the array \'AATT\' and hash \'%\' sigils are passed by reference, i.e. under the default bindings:
AATTarray->join(\', \') AATT{ ... }->length() %hash->keys() %$hash->values()
are equivalent to:
ARRAY::join(\\AATTarray, \', \') ARRAY::length(\\AATT{ ... }) HASH::keys(\\%hash) HASH::values(\\%$hash)
Multiple \'use autobox\' statements can appear in the same scope. These are merged both \"horizontally\" (i.e. multiple classes can be associated with a particular type) and \"vertically\" (i.e. multiple classes can be associated with multiple types).
Thus:
use autobox SCALAR => \'Foo\'; use autobox SCALAR => \'Bar\';
- associates SCALAR types with a synthetic class whose \'AATTISA\' includes both Foo and Bar (in that order).
Likewise:
use autobox SCALAR => \'Foo\'; use autobox SCALAR => \'Bar\'; use autobox ARRAY => \'Baz\';
and
use autobox SCALAR => [ \'Foo\', \'Bar\' ]; use autobox ARRAY => \'Baz\';
- bind SCALAR types to the Foo and Bar classes and ARRAY types to Baz.
autobox is lexically scoped, and bindings for an outer scope can be extended or countermanded in a nested scope:
{ use autobox; # default bindings: autobox all native types ...
{ use autobox SCALAR => \'MyScalar\'; ... }
... }
Autoboxing can be turned off entirely by using the \'no\' syntax:
{ use autobox; ... no autobox; ... }
- or can be selectively disabled by passing arguments to the \'no autobox\' statement:
use autobox; # default bindings
no autobox qw(SCALAR);
[]->foo(); # OK: ARRAY::foo([])
\"Hello, world!\"->bar(); # runtime error
Autoboxing is not performed for barewords, i.e.
my $foo = Foo->new();
and:
my $foo = new Foo;
behave as expected.
Methods are called on native types by means of the arrow operator. As with regular objects, the right hand side of the operator can either be a bare method name or a variable containing a method name or subroutine reference. Thus the following are all valid:
sub method1 { ... } my $method2 = \'some_method\'; my $method3 = sub { ... }; my $method4 = \\&some_method;
\" ... \"->method1(); [ ... ]->$method2(); { ... }->$method3(); sub { ... }->$method4();
A native type is only associated 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\';
AATTarray->some_array_method();
- as no class is specified for the ARRAY type. Note: the result of calling a method on a native type 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 native 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 AATT{$_[0]} }
print \"Hello, world!\"->split->length();
However, using these default bindings is not recommended as there\'s no guarantee that another piece of code won\'t trample over the same namespace/methods.
|
RPM found in directory: /packages/linux-pbone/ftp5.gwdg.de/pub/opensuse/repositories/devel:/languages:/perl:/CPAN-A/openSUSE_Tumbleweed/x86_64 |