/usr/lib/perl5/autobox.pod is in libautobox-perl 2.82-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 | =pod
=head1 NAME
autobox - call methods on native types
=head1 SYNOPSIS
use autobox;
# 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 @list = 'SELECT * FROM foo'->list();
my $greeting = "Hello, world!"->upper(); # "HELLO, WORLD!"
$greeting->for_each(\&character_handler);
# arrays and array refs
my $schwartzian = @_->map(...)->sort(...)->map(...);
my $hash = [ 'SELECT * FROM foo WHERE id IN (?, ?)', 1, 2 ]->hash();
# hashes and hash refs
{ alpha => 'beta', gamma => 'vlissides' }->for_each(...);
%hash->keys();
# code refs
my $plus_five = (\&add)->curry()->(5);
my $minus_three = sub { $_[0] - $_[1] }->reverse->curry->(3);
# can, isa, VERSION, import and unimport can be accessed via autobox_class
42->autobox_class->isa('MyNumber')
say []->autobox_class->VERSION
=head1 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.
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 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 C<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 C<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 C<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 C<@> and hash C<%> sigils are passed by reference, i.e. under the default bindings:
@array->join(', ')
@{ ... }->length()
%hash->keys()
%$hash->values()
are equivalent to:
ARRAY::join(\@array, ', ')
ARRAY::length(\@{ ... })
HASH::keys(\%hash)
HASH::values(\%$hash)
Multiple C<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 C<@ISA> includes both C<Foo> and C<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 C<Foo> and C<Bar> classes and ARRAY types to C<Baz>.
C<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
...
{
# appends 'MyScalar' to the @ISA associated with SCALAR types
use autobox SCALAR => 'MyScalar';
...
}
# back to the default (no MyScalar)
...
}
Autoboxing can be turned off entirely by using the C<no> syntax:
{
use autobox;
...
no autobox;
...
}
- or can be selectively disabled by passing arguments to the C<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 L<arrow operator|perlop/"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 C<use autobox> statement. Thus the following will not work:
use autobox SCALAR => 'MyScalar';
@array->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 C<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 @{$_[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.
=head1 OPTIONS
A mapping from native types to their user-defined classes can be specified
by passing a hashref or a list of key/value pairs to the C<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 => [ 'MyHash', 'MyNamespace::' ], # one or more class names and/or prefixes
CODE => ..., # any of the 3 value types above
INTEGER => ..., # any of the 3 value types above
FLOAT => ..., # any of the 3 value types above
NUMBER => ..., # any of the 3 value types above
STRING => ..., # any of the 3 value types above
UNDEF => ..., # any of the 3 value types above
UNIVERSAL => ..., # any of the 3 value types above
DEFAULT => ..., # any of the 3 value types above
DEBUG => ... # boolean or coderef
};
The INTEGER, FLOAT, NUMBER, STRING, SCALAR, ARRAY, HASH, CODE, UNDEF, DEFAULT and UNIVERSAL options can take
three different types of value:
=over
=item *
A class name e.g.
use autobox INTEGER => 'MyInt';
This binds the specified native type to the specified class. All methods invoked on
literals or values of type C<key> will be dispatched as methods of the class specified in
the corresponding C<value>.
=item *
A namespace: this is a class prefix (up to and including the final '::')
to which the specified type name (INTEGER, FLOAT, STRING &c.) will be appended:
Thus:
use autobox ARRAY => 'Prelude::';
is equivalent to:
use autobox ARRAY => 'Prelude::ARRAY';
=item *
A reference to an array of class names and/or namespaces. This associates multiple classes with the
specified type.
=back
=head2 DEFAULT
The C<DEFAULT> option specifies bindings for any of the four default types (SCALAR, ARRAY, HASH and CODE)
not supplied in the C<use autobox> statement. As with the other options, the C<value> corresponding to
the C<DEFAULT> C<key> can be a class name, a namespace, or a reference to an array containing one or
more class names and/or namespaces.
Thus:
use autobox
STRING => 'MyString',
DEFAULT => 'MyDefault';
is equivalent to:
use autobox
STRING => 'MyString',
SCALAR => 'MyDefault',
ARRAY => 'MyDefault',
HASH => 'MyDefault',
CODE => 'MyDefault';
Which in turn is equivalent to:
use autobox
INTEGER => 'MyDefault',
FLOAT => 'MyDefault',
STRING => [ 'MyString', 'MyDefault' ],
ARRAY => 'MyDefault',
HASH => 'MyDefault',
CODE => 'MyDefault';
Namespaces in DEFAULT values have the default type name appended, which, in the case of defaulted SCALAR types,
is SCALAR rather than INTEGER, FLOAT &c.
Thus:
use autobox
ARRAY => 'MyArray',
HASH => 'MyHash',
CODE => 'MyCode',
DEFAULT => 'MyNamespace::';
is equivalent to:
use autobox
INTEGER => 'MyNamespace::SCALAR',
FLOAT => 'MyNamespace::SCALAR',
STRING => 'MyNamespace::SCALAR',
ARRAY => 'MyArray',
HASH => 'MyArray',
CODE => 'MyCode';
Any of the four default types can be exempted from defaulting to the DEFAULT value by supplying a value of undef:
use autobox
HASH => undef,
DEFAULT => 'MyDefault';
42->foo # ok: MyDefault::foo
[]->bar # ok: MyDefault::bar
%INC->baz # not ok: runtime error
=head2 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 => 'MyUndef';
undef->foo(); # ok
So does this:
use autobox UNDEF => 'MyNamespace::';
undef->foo(); # ok
=head2 NUMBER, SCALAR and UNIVERSAL
The virtual types NUMBER, SCALAR and UNIVERSAL function as macros or shortcuts which create
bindings for their subtypes. The type hierarchy is as follows:
UNIVERSAL -+
|
+- SCALAR -+
| |
| +- NUMBER -+
| | |
| | +- INTEGER
| | |
| | +- FLOAT
| |
| +- STRING
|
+- ARRAY
|
+- HASH
|
+- CODE
Thus:
use autobox NUMBER => 'MyNumber';
is equivalent to:
use autobox
INTEGER => 'MyNumber',
FLOAT => 'MyNumber';
And:
use autobox SCALAR => 'MyScalar';
is equivalent to:
use autobox
INTEGER => 'MyScalar',
FLOAT => 'MyScalar',
STRING => 'MyScalar';
Virtual types can also be passed to C<unimport> via the C<no autobox> syntax. This disables autoboxing
for the corresponding subtypes e.g.
no autobox qw(NUMBER);
is equivalent to:
no autobox qw(INTEGER FLOAT);
Virtual type bindings can be mixed with ordinary bindings to provide fine-grained control over
inheritance and delegation. For instance:
use autobox
INTEGER => 'MyInteger',
NUMBER => 'MyNumber',
SCALAR => 'MyScalar';
would result in the following bindings:
42->foo -> [ MyInteger, MyNumber, MyScalar ]
3.1415927->bar -> [ MyNumber, MyScalar ]
"Hello, world!->baz -> [ MyScalar ]
Note that DEFAULT bindings take precedence over virtual type bindings i.e.
use autobox
UNIVERSAL => 'MyUniversal',
DEFAULT => 'MyDefault'; # default SCALAR, ARRAY, HASH and CODE before UNIVERSAL
is equivalent to:
use autobox
INTEGER => [ 'MyDefault', 'MyUniversal' ],
FLOAT => [ 'MyDefault', 'MyUniversal' ], # ... &c.
=head2 DEBUG
C<DEBUG> exposes the current bindings for the scope in which C<use autobox> is called by means
of a callback, or a static debugging function.
This allows the computed bindings to be seen in "longhand".
The option is ignored if the value corresponding to the C<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 bindings for the current scope.
Finally, if C<DEBUG> is true but not a CODE ref, the bindings 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, ...
=head1 METHODS
=head2 import
This method sets up C<autobox> bindings for the current lexical scope. It can be used to implement
C<autobox> extensions i.e. lexically-scoped modules that provide C<autobox> bindings for one or more
native types without requiring calling code to C<use autobox>.
This is done by subclassing C<autobox> and overriding C<import>. This allows extensions to effectively
translate C<use MyModule> into a bespoke C<use autobox> call. e.g.:
package String::Trim;
use base qw(autobox);
sub import {
my $class = shift;
$class->SUPER::import(
STRING => 'String::Trim::String'
);
}
package String::Trim::String;
sub trim {
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
$string;
}
1;
Note that C<trim> is defined in an auxiliary class rather than in C<String::Trim> itself to prevent
C<String::Trim>'s own methods (i.e. the methods it inherits from C<autobox>) being exposed to C<STRING> types.
This module can now be used without a C<use autobox> statement to enable the C<trim> method in the current
lexical scope. e.g.:
#!/usr/bin/env perl
use String::Trim;
print " Hello, world! "->trim();
=head1 UNIVERSAL METHODS FOR AUTOBOXED TYPES
=head2 autobox_class
C<autobox> adds a single method to all autoboxed types: C<autobox_class>. This can be used to
call C<can>, C<isa>, C<VERSION>, C<import> and C<unimport>. e.g.
if (sub { ... }->autobox_class->can('curry')) ...
if (42->autobox_class->isa('SCALAR')) ...
Note: C<autobox_class> should B<always> be used when calling these methods. The behaviour when
these methods are called directly on the native type e.g.:
42->can('foo')
42->isa('Bar')
42->VERSION
- is undefined.
=head1 EXPORTS
=head2 type
C<autobox> includes an additional module, C<autobox::universal>, which exports a single subroutine, C<type>.
This sub returns the type of its argument within C<autobox> (which is essentially longhand for the type names
used within perl). This value is used by C<autobox> to associate a method invocant with its designated classes. e.g.
use autobox::universal qw(type);
type("Hello, world!") # STRING
type(42) # INTEGER
type([]) # ARRAY
type(sub { }) # CODE
C<autobox::universal> is loaded automatically by C<autobox>, and, as its name suggests, can be used to install
a universal method (i.e. a method for all C<autobox> types) e.g.
use autobox UNIVERSAL => 'autobox::universal';
42->type # INTEGER
3.1415927->type # FLOAT
%ENV->type # HASH
=head1 CAVEATS
=head2 Performance
Autoboxing comes at a price. Calling
"Hello, world!"->length()
is slightly slower than the equivalent method call on a string-like object, and significantly slower than
length("Hello, world!")
=head2 Gotchas
=head3 Precedence
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);
=head3 print BLOCK
Perl's special-casing for the C<print BLOCK ...> syntax (see L<perlsub>) means that C<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 C<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);
=head3 eval EXPR
Like most pragmas, autobox performs operations at compile time, and,
as a result, runtime string C<eval>s are not executed within its scope i.e. this
doesn't work:
use autobox;
eval "42->foo";
The workaround is to use autobox within the C<eval> e.g.
eval <<'EOS';
use autobox;
42->foo();
EOS
Note that the C<eval BLOCK> form works as expected:
use autobox;
eval { 42->foo() }; # OK
=head1 VERSION
2.82
=head1 SEE ALSO
=over
=item * L<autobox::Core|autobox::Core>
=item * L<Moose::Autobox>
=item * L<perl5i|perl5i>
=item * L<Scalar::Properties|Scalar::Properties>
=back
=head1 AUTHOR
chocolateboy <chocolate@cpan.org>
=head1 COPYRIGHT
Copyright (c) 2003-2013, chocolateboy.
This module is free software. It may be used, redistributed
and/or modified under the same terms as Perl itself.
=cut
|