1 .\" Automatically generated by Pod::Man 2.22 (Pod::Simple 3.10)
4 .\" ========================================================================
5 .de Sp \" Vertical space (when we can't use .PP)
9 .de Vb \" Begin verbatim text
14 .de Ve \" End verbatim text
18 .\" Set up some character translations and predefined strings. \*(-- will
19 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
20 .\" double quote, and \*(R" will give a right double quote. \*(C+ will
21 .\" give a nicer C++. Capital omega is used to do unbreakable dashes and
22 .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
23 .\" nothing in troff, for use with C<>.
25 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
29 . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
30 . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
43 .\" Escape single quotes in literal strings from groff's Unicode transform.
47 .\" If the F register is turned on, we'll generate index entries on stderr for
48 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
49 .\" entries marked with X<> in POD. Of course, you'll have to process the
50 .\" output yourself in some meaningful fashion.
53 . tm Index:\\$1\t\\n%\t"\\$2"
63 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
64 .\" Fear. Run. Save yourself. No user-serviceable parts.
65 . \" fudge factors for nroff and troff
74 . ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80 . \" simple accents for nroff and troff
90 . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
91 . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
92 . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
93 . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
94 . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
95 . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
97 . \" troff and (daisy-wheel) nroff accents
98 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
99 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
100 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
101 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
102 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
103 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
104 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
105 .ds ae a\h'-(\w'a'u*4/10)'e
106 .ds Ae A\h'-(\w'A'u*4/10)'E
107 . \" corrections for vroff
108 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
109 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
110 . \" for low resolution devices (crt and lpr)
111 .if \n(.H>23 .if \n(.V>19 \
124 .\" ========================================================================
126 .IX Title "autobox 3"
127 .TH autobox 3 "2008-05-25" "perl v5.8.7" "User Contributed Perl Documentation"
128 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
129 .\" way too many mistakes in technical documents.
133 autobox \- call methods on native types
135 .IX Header "SYNOPSIS"
141 \& my $range = 10\->to(1); # [ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ]
145 \& my $error = 3.1415927\->minus(22/7)\->abs();
149 \& my @list = \*(AqSELECT * FROM foo\*(Aq\->list();
150 \& my $greeting = "Hello, world!"\->upper(); # "HELLO, WORLD!"
152 \& $greeting\->for_each(\e&character_handler);
154 \& # arrays and array refs
156 \& my $schwartzian = @_\->map(...)\->sort(...)\->map(...);
157 \& my $hash = [ \*(AqSELECT * FROM foo WHERE id IN (?, ?)\*(Aq, 1, 2 ]\->hash();
159 \& # hashes and hash refs
161 \& { alpha => \*(Aqbeta\*(Aq, gamma => \*(Aqvlissides\*(Aq }\->for_each(...);
166 \& my $plus_five = (\e&add)\->curry()\->(5);
167 \& my $minus_three = sub { $_[0] \- $_[1] }\->reverse\->curry\->(3);
169 \& # can() and isa() work as expected
171 \& if ("Hello, world!"\->can(\*(Aqfoo\*(Aq)) ...
172 \& if (3.1415927\->isa(\*(AqSCALAR\*(Aq)) ...
175 .IX Header "DESCRIPTION"
176 The autobox pragma allows methods to be called on integers, floats, strings, arrays,
177 hashes, and code references in exactly the same manner as blessed references.
179 The autoboxing is transparent: boxed values are not blessed into their (user-defined)
180 implementation class (unless the method elects to bestow such a blessing) \- they simply
181 use its methods as though they are.
183 The classes (packages) into which the native types are boxed are fully configurable.
184 By default, a method invoked on a non-object is assumed to be
185 defined in a class whose name corresponds to the \f(CW\*(C`ref()\*(C'\fR type of that
186 value \- or \s-1SCALAR\s0 if the value is a non-reference.
188 This mapping can be overriden by passing key/value pairs to the \f(CW\*(C`use autobox\*(C'\fR
189 statement, in which the keys represent native types, and the values
190 their associated classes.
192 As with regular objects, autoboxed values are passed as the first argument of the specified method.
193 Consequently, given a vanilla \f(CW\*(C`use autobox\*(C'\fR:
196 \& "Hello, world!"\->upper()
202 \& SCALAR::upper("hello, world!")
208 \& [ 1 .. 10 ]\->for_each(sub { ... })
214 \& ARRAY::for_each([ 1 .. 10 ], sub { ... })
217 Values beginning with the array \f(CW\*(C`@\*(C'\fR and hash \f(CW\*(C`%\*(C'\fR sigils are passed by reference, i.e. under the default bindings:
220 \& @array\->join(\*(Aq, \*(Aq)
221 \& @{ ... }\->length()
229 \& ARRAY::join(\e@array, \*(Aq, \*(Aq)
230 \& ARRAY::length(\e@{ ... })
231 \& HASH::keys(\e%hash)
232 \& HASH::values(\e%$hash)
235 Multiple \f(CW\*(C`use autobox\*(C'\fR statements can appear in the same scope. These are merged both \*(L"horizontally\*(R" (i.e.
236 mutiple classes can be associated with a particular type) and \*(L"vertically\*(R" (i.e. multiple classes can be associated
237 with multiple types).
242 \& use autobox SCALAR => \*(AqFoo\*(Aq;
243 \& use autobox SCALAR => \*(AqBar\*(Aq;
246 \&\- associates \s-1SCALAR\s0 types with a synthetic class whose \f(CW@ISA\fR includes both \f(CW\*(C`Foo\*(C'\fR and \f(CW\*(C`Bar\*(C'\fR (in that order).
251 \& use autobox SCALAR => \*(AqFoo\*(Aq;
252 \& use autobox SCALAR => \*(AqBar\*(Aq;
253 \& use autobox ARRAY => \*(AqBaz\*(Aq;
259 \& use autobox SCALAR => [ \*(AqFoo\*(Aq, \*(AqBar\*(Aq ];
260 \& use autobox ARRAY => \*(AqBaz\*(Aq;
263 \&\- bind \s-1SCALAR\s0 types to the \f(CW\*(C`Foo\*(C'\fR and \f(CW\*(C`Bar\*(C'\fR classes and \s-1ARRAY\s0 types to \f(CW\*(C`Baz\*(C'\fR.
265 \&\f(CW\*(C`autobox\*(C'\fR is lexically scoped, and bindings for an outer scope
266 can be extended or countermanded in a nested scope:
270 \& use autobox; # default bindings: autobox all native types
274 \& # appends \*(AqMyScalar\*(Aq to the @ISA associated with SCALAR types
275 \& use autobox SCALAR => \*(AqMyScalar\*(Aq;
279 \& # back to the default (no MyScalar)
284 Autoboxing can be turned off entirely by using the \f(CW\*(C`no\*(C'\fR syntax:
295 \&\- or can be selectively disabled by passing arguments to the \f(CW\*(C`no autobox\*(C'\fR statement:
298 \& use autobox; # default bindings
300 \& no autobox qw(SCALAR);
302 \& []\->foo(); # OK: ARRAY::foo([])
304 \& "Hello, world!"\->bar(); # runtime error
307 Autoboxing is not performed for barewords i.e.
310 \& my $foo = Foo\->new();
316 \& my $foo = new Foo;
321 Methods are called on native types by means of the arrow operator. As with
322 regular objects, the right hand side of the operator can either be a bare method name or a variable containing
323 a method name or subroutine reference. Thus the following are all valid:
326 \& sub method1 { ... }
327 \& my $method2 = \*(Aqsome_method\*(Aq;
328 \& my $method3 = sub { ... };
329 \& my $method4 = \e&some_method;
331 \& " ... "\->method1();
332 \& [ ... ]\->$method2();
333 \& { ... }\->$method3();
334 \& sub { ... }\->$method4();
337 A native type is only asociated with a class if the type => class mapping
338 is supplied in the \f(CW\*(C`use autobox\*(C'\fR statement. Thus the following will not work:
341 \& use autobox SCALAR => \*(AqMyScalar\*(Aq;
343 \& @array\->some_array_method();
346 \&\- as no class is specified for the \s-1ARRAY\s0 type. Note: the result of calling a method
347 on a native type that is not associated with a class is the usual runtime error message:
350 \& Can\*(Aqt call method "some_array_method" on unblessed reference at ...
353 As a convenience, there is one exception to this rule. If \f(CW\*(C`use autobox\*(C'\fR is invoked with no arguments
354 (ignoring the \s-1DEBUG\s0 option) the four main native types are associated with classes of the same name.
362 \&\- is equivalent to:
366 \& SCALAR => \*(AqSCALAR\*(Aq,
367 \& ARRAY => \*(AqARRAY\*(Aq,
368 \& HASH => \*(AqHASH\*(Aq,
369 \& CODE => \*(AqCODE\*(Aq;
372 This facilitates one-liners and prototypes:
377 \& sub SCALAR::split { [ split \*(Aq\*(Aq, $_[0] ] }
378 \& sub ARRAY::length { scalar @{$_[0]} }
380 \& print "Hello, world!"\->split\->length();
383 However, using these default bindings is not recommended as there's no guarantee that another
384 piece of code won't trample over the same namespace/methods.
387 A mapping from native types to their user-defined classes can be specified
388 by passing a list of key/value pairs to the \f(CW\*(C`use autobox\*(C'\fR statement.
390 The following example shows the range of valid arguments:
394 \& SCALAR => \*(AqMyScalar\*(Aq # class name
395 \& ARRAY => \*(AqMyNamespace::\*(Aq, # class prefix (ending in \*(Aq::\*(Aq)
396 \& HASH => [ \*(AqMyHash\*(Aq, \*(AqMyNamespace::\*(Aq ], # one or more class names and/or prefixes
397 \& CODE => ..., # any of the 3 value types above
398 \& INTEGER => ..., # any of the 3 value types above
399 \& FLOAT => ..., # any of the 3 value types above
400 \& NUMBER => ..., # any of the 3 value types above
401 \& STRING => ..., # any of the 3 value types above
402 \& UNDEF => ..., # any of the 3 value types above
403 \& UNIVERSAL => ..., # any of the 3 value types above
404 \& DEFAULT => ..., # any of the 3 value types above
405 \& DEBUG => ...; # boolean or coderef
408 The \s-1INTEGER\s0, \s-1FLOAT\s0, \s-1NUMBER\s0, \s-1STRING\s0, \s-1SCALAR\s0, \s-1ARRAY\s0, \s-1HASH\s0, \s-1CODE\s0, \s-1UNDEF\s0, \s-1DEFAULT\s0 and \s-1UNIVERSAL\s0 options can take
409 three different types of value:
414 \& use autobox INTEGER => \*(AqMyInt\*(Aq;
417 This binds the specified native type to the specified class. All methods invoked on
418 literals or values of type \f(CW\*(C`key\*(C'\fR will be dispatched as methods of the class specified in
419 the corresponding \f(CW\*(C`value\*(C'\fR.
421 A namespace: this is a class prefix (up to and including the final '::')
422 to which the specified type name (\s-1INTEGER\s0, \s-1FLOAT\s0, \s-1STRING\s0 &c.) will be appended:
427 \& use autobox ARRAY => \*(AqPrelude::\*(Aq;
433 \& use autobox ARRAY => \*(AqPrelude::ARRAY\*(Aq;
436 A reference to an array of class names and/or namespaces. This associates multiple classes with the
439 .IX Subsection "DEFAULT"
440 The \f(CW\*(C`DEFAULT\*(C'\fR option specifies bindings for any of the four default types (\s-1SCALAR\s0, \s-1ARRAY\s0, \s-1HASH\s0 and \s-1CODE\s0)
441 not supplied in the \f(CW\*(C`use autobox\*(C'\fR statement. As with the other options, the \f(CW\*(C`value\*(C'\fR corresponding to
442 the \f(CW\*(C`DEFAULT\*(C'\fR \f(CW\*(C`key\*(C'\fR can be a class name, a namespace, or a reference to an array containing one or
443 more class names and/or namespaces.
449 \& STRING => \*(AqMyString\*(Aq,
450 \& DEFAULT => \*(AqMyDefault\*(Aq;
457 \& STRING => \*(AqMyString\*(Aq,
458 \& SCALAR => \*(AqMyDefault\*(Aq,
459 \& ARRAY => \*(AqMyDefault\*(Aq,
460 \& HASH => \*(AqMyDefault\*(Aq,
461 \& CODE => \*(AqMyDefault\*(Aq;
464 Which in turn is equivalent to:
468 \& INTEGER => \*(AqMyDefault\*(Aq,
469 \& FLOAT => \*(AqMyDefault\*(Aq,
470 \& STRING => [ \*(AqMyString\*(Aq, \*(AqMyDefault\*(Aq ],
471 \& ARRAY => \*(AqMyDefault\*(Aq,
472 \& HASH => \*(AqMyDefault\*(Aq,
473 \& CODE => \*(AqMyDefault\*(Aq;
476 Namespaces in \s-1DEFAULT\s0 values have the default type name appended, which, in the case of defaulted \s-1SCALAR\s0 types,
477 is \s-1SCALAR\s0 rather than \s-1INTEGER\s0, \s-1FLOAT\s0 &c.
483 \& ARRAY => \*(AqMyArray\*(Aq,
484 \& HASH => \*(AqMyHash\*(Aq,
485 \& CODE => \*(AqMyCode\*(Aq,
486 \& DEFAULT => \*(AqMyNamespace::\*(Aq;
493 \& INTEGER => \*(AqMyNamespace::SCALAR\*(Aq,
494 \& FLOAT => \*(AqMyNamespace::SCALAR\*(Aq,
495 \& STRING => \*(AqMyNamespace::SCALAR\*(Aq,
496 \& ARRAY => \*(AqMyArray\*(Aq,
497 \& HASH => \*(AqMyArray\*(Aq,
498 \& CODE => \*(AqMyCode\*(Aq;
501 Any of the four default types can be exempted from defaulting to the \s-1DEFAULT\s0 value by supplying a value of undef:
506 \& DEFAULT => \*(AqMyDefault\*(Aq;
508 \& 42\->foo # ok: MyDefault::foo
509 \& []\->bar # ok: MyDefault::bar
511 \& %INC\->baz # not ok: runtime error
514 .IX Subsection "UNDEF"
515 The pseudotype, \s-1UNDEF\s0, can be used to autobox undefined values. These are not autoboxed by default.
522 \& undef\->foo() # runtime error
528 \& use autobox UNDEF => \*(AqMyUndef\*(Aq;
530 \& undef\->foo(); # ok
536 \& use autobox UNDEF => \*(AqMyNamespace::\*(Aq;
538 \& undef\->foo(); # ok
540 .SS "\s-1NUMBER\s0, \s-1SCALAR\s0 and \s-1UNIVERSAL\s0"
541 .IX Subsection "NUMBER, SCALAR and UNIVERSAL"
542 The virtual types \s-1NUMBER\s0, \s-1SCALAR\s0 and \s-1UNIVERSAL\s0 function as macros or shortcuts which create
543 bindings for their subtypes. The type hierarchy is as follows:
568 \& use autobox NUMBER => \*(AqMyNumber\*(Aq;
575 \& INTEGER => \*(AqMyNumber\*(Aq,
576 \& FLOAT => \*(AqMyNumber\*(Aq;
582 \& use autobox SCALAR => \*(AqMyScalar\*(Aq;
589 \& INTEGER => \*(AqMyScalar\*(Aq,
590 \& FLOAT => \*(AqMyScalar\*(Aq,
591 \& STRING => \*(AqMyScalar\*(Aq;
594 Virtual types can also be passed to \f(CW\*(C`unimport\*(C'\fR via the \f(CW\*(C`no autobox\*(C'\fR syntax. This disables autoboxing
595 for the corresponding subtypes e.g.
598 \& no autobox qw(NUMBER);
604 \& no autobox qw(INTEGER FLOAT);
607 Virtual type bindings can be mixed with ordinary bindings to provide fine-grained control over
608 inheritance and delegation. For instance:
612 \& INTEGER => \*(AqMyInteger\*(Aq,
613 \& NUMBER => \*(AqMyNumber\*(Aq,
614 \& SCALAR => \*(AqMyScalar\*(Aq;
617 would result in the following bindings:
620 \& 42\->foo \-> [ MyInteger, MyNumber, MyScalar ]
621 \& 3.1415927\->bar \-> [ MyNumber, MyScalar ]
622 \& "Hello, world!\->baz \-> [ MyScalar ]
625 Note that \s-1DEFAULT\s0 bindings take precedence over virtual type bindings i.e.
629 \& UNIVERSAL => \*(AqMyUniversal\*(Aq,
630 \& DEFAULT => \*(AqMyDefault\*(Aq; # default SCALAR, ARRAY, HASH and CODE before UNIVERSAL
637 \& INTEGER => [ \*(AqMyDefault\*(Aq, \*(AqMyUniversal\*(Aq ],
638 \& FLOAT => [ \*(AqMyDefault\*(Aq, \*(AqMyUniversal\*(Aq ], # ... &c.
641 .IX Subsection "DEBUG"
642 \&\f(CW\*(C`DEBUG\*(C'\fR exposes the current bindings for the scope in which \f(CW\*(C`use autobox\*(C'\fR is called by means
643 of a callback, or a static debugging function.
645 This allows the computed bindings to be seen in \*(L"longhand\*(R".
647 The option is ignored if the value corresponding to the \f(CW\*(C`DEBUG\*(C'\fR key is false.
649 If the value is a \s-1CODE\s0 ref, then this sub is called with a reference to
650 the hash containing the computed bindings for the current scope.
652 Finally, if \f(CW\*(C`DEBUG\*(C'\fR is true but not a \s-1CODE\s0 ref, the bindings are dumped
658 \& use autobox DEBUG => 1, ...
664 \& use autobox DEBUG => sub { ... }, ...
670 \& sub my_callback ($) {
671 \& my $hashref = shift;
675 \& use autobox DEBUG => \e&my_callback, ...
680 .IX Subsection "import"
681 On its own, \f(CW\*(C`autobox\*(C'\fR doesn't implement any methods that can be called on native types.
682 However, its static method, \f(CW\*(C`import\*(C'\fR, can be used to implement \f(CW\*(C`autobox\*(C'\fR extensions i.e.
683 lexically scoped modules that provide \f(CW\*(C`autobox\*(C'\fR bindings for one or more native types without requiring
684 calling code to \f(CW\*(C`use autobox\*(C'\fR.
686 This is done by subclassing \f(CW\*(C`autobox\*(C'\fR and overriding \f(CW\*(C`import\*(C'\fR. This allows extensions to effectively
687 translate \f(CW\*(C`use MyModule\*(C'\fR into a bespoke \f(CW\*(C`use autobox\*(C'\fR call. e.g.:
690 \& package String::Trim;
692 \& use base qw(autobox);
695 \& my $class = shift;
696 \& $class\->SUPER::import(STRING => \*(AqString::Trim::Scalar\*(Aq);
699 \& package String::Trim::Scalar;
702 \& my $string = shift;
703 \& $string =~ s/^\es+//;
704 \& $string =~ s/\es+$//;
711 Note that \f(CW\*(C`trim\*(C'\fR is defined in an auxilliary class rather than in \f(CW\*(C`String::Trim\*(C'\fR itself to prevent
712 \&\f(CW\*(C`String::Trim\*(C'\fR's own methods (i.e. the methods it inherits from \f(CW\*(C`autobox\*(C'\fR) being exposed to \s-1SCALAR\s0 types.
714 This module can now be used without a \f(CW\*(C`use autobox\*(C'\fR statement to enable the \f(CW\*(C`trim\*(C'\fR method in the current
718 \& #!/usr/bin/env perl
722 \& print " Hello, world! "\->trim();
726 Although \f(CW\*(C`autobox\*(C'\fR doesn't export anything, it includes an additional module, \f(CW\*(C`autobox::universal\*(C'\fR,
727 which exports a single subroutine, \f(CW\*(C`type\*(C'\fR.
729 .IX Subsection "type"
730 This sub returns the type of its argument within \f(CW\*(C`autobox\*(C'\fR (which is essentially longhand for the type names
731 used within perl). This value is used by \f(CW\*(C`autobox\*(C'\fR to associate a method invocant with its designated classes. e.g.
734 \& use autobox::universal qw(type);
736 \& type("Hello, world!") # STRING
737 \& type(42) # INTEGER
739 \& type(sub { }) # CODE
742 \&\f(CW\*(C`autobox::universal\*(C'\fR is loaded automatically by \f(CW\*(C`autobox\*(C'\fR, and, as its name suggests, can be used to install
743 a universal method (i.e. a method for all \f(CW\*(C`autobox\*(C'\fR types) e.g.
746 \& use autobox UNIVERSAL => \*(Aqautobox::universal\*(Aq;
748 \& 42\->type # INTEGER
749 \& 3.1415927\->type # FLOAT
750 \& %ENV\->type # HASH
755 .IX Subsection "Performance"
756 Autoboxing comes at a price. Calling
759 \& "Hello, world!"\->length()
762 is slightly slower than the equivalent method call on a string-like object, and significantly slower than
765 \& length("Hello, world!")
768 .IX Subsection "Gotchas"
770 .IX Subsection "Precedence"
772 Due to Perl's precedence rules, some autoboxed literals may need to be parenthesized:
774 For instance, while this works:
777 \& my $curried = sub { ... }\->curry();
783 \& my $curried = \e&foo\->curry();
786 The solution is to wrap the reference in parentheses:
789 \& my $curried = (\e&foo)\->curry();
792 The same applies for signed integer and float literals:
796 \& my $range = 10\->to(1);
798 \& # this doesn\*(Aqt work
799 \& my $range = \-10\->to(10);
802 \& my $range = (\-10)\->to(10);
805 \fIprint \s-1BLOCK\s0\fR
806 .IX Subsection "print BLOCK"
808 Perl's special-casing for the \f(CW\*(C`print BLOCK ...\*(C'\fR syntax (see perlsub) means that \f(CW\*(C`print { expression() } ...\*(C'\fR
809 (where the curly brackets denote an anonymous \s-1HASH\s0 ref) may require some further disambiguation:
813 \& print { foo => \*(Aqbar\*(Aq }\->foo();
816 \& print { \*(Aqfoo\*(Aq, \*(Aqbar\*(Aq }\->foo();
819 \& print { \*(Aqfoo\*(Aq, \*(Aqbar\*(Aq, @_ }\->foo();
821 \& # but this doesn\*(Aqt
822 \& print { @_ }\->foo() ? 1 : 0
825 In the latter case, the solution is to supply something
826 other than a \s-1HASH\s0 ref literal as the first argument
827 to \f(CW\*(C`print()\*(C'\fR:
831 \& print STDOUT { @_ }\->foo() ? 1 : 0;
834 \& my $hashref = { @_ };
835 \& print $hashref\->foo() ? 1 : 0;
838 \& print \*(Aq\*(Aq, { @_ }\->foo() ? 1 : 0;
841 \& print \*(Aq\*(Aq . { @_ }\->foo() ? 1 : 0;
844 \& { @_ }\->print_if_foo(1, 0);
847 \fI\s-1VERSION\s0, import and unimport\fR
848 .IX Subsection "VERSION, import and unimport"
850 Although \f(CW\*(C`can\*(C'\fR and \f(CW\*(C`isa\*(C'\fR work as expected for autoboxed values, the \f(CW\*(C`VERSION\*(C'\fR method doesn't.
851 Thus, while these work:
854 \& [ ... ]\->can(\*(Aqpop\*(Aq)
856 \& 3.1415\->isa(\*(AqMyScalar\*(Aq)
862 \& use MyScalar 1.23;
863 \& use autobox SCALAR => \*(AqMyScalar\*(Aq;
865 \& print "Hello, world!"\->VERSION();
871 \& print MyScalar\->VERSION();
877 \& print $MyScalar::VERSION;
882 Likewise, \f(CW\*(C`import\*(C'\fR and \f(CW\*(C`unimport\*(C'\fR are unaffected by the autobox pragma:
885 \& # equivalent to Foo\->import() rather than MyScalar\->import(\*(AqFoo\*(Aq)
886 \& \*(AqFoo\*(Aq\->import()
888 \& # error: Can\*(Aqt call method "import" on unblessed reference
892 \fIeval \s-1EXPR\s0\fR
893 .IX Subsection "eval EXPR"
895 Like most pragmas autobox performs some of its operations at compile time, and,
896 as a result, runtime string \f(CW\*(C`eval\*(C'\fRs are not executed within its scope i.e. this
905 The workaround is to use autobox within the \f(CW\*(C`eval\*(C'\fR e.g.
908 \& eval <<\*(AqEOS\*(Aq;
914 Note that the \f(CW\*(C`eval BLOCK\*(C'\fR form works as expected:
919 \& eval { 42\->foo() }; # OK
925 .IX Header "SEE ALSO"
927 autobox::Closure::Attributes
931 autobox::DateTime::Duration
944 chocolateboy <chocolate.boy@email.com>
946 .IX Header "COPYRIGHT"
947 Copyright (c) 2003\-2008, chocolateboy.
949 This module is free software. It may be used, redistributed
950 and/or modified under the same terms as Perl itself.