{"id":7030,"date":"2022-12-20T19:34:41","date_gmt":"2022-12-20T22:34:41","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/autobox-man3\/"},"modified":"2022-12-20T19:34:41","modified_gmt":"2022-12-20T22:34:41","slug":"autobox-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/autobox-man3\/","title":{"rendered":"autobox (man3)"},"content":{"rendered":"<h1 align=\"center\">autobox<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#OPTIONS\">OPTIONS<\/a><br \/> <a href=\"#METHODS\">METHODS<\/a><br \/> <a href=\"#UNIVERSAL METHODS FOR AUTOBOXED TYPES\">UNIVERSAL METHODS FOR AUTOBOXED TYPES<\/a><br \/> <a href=\"#EXPORTS\">EXPORTS<\/a><br \/> <a href=\"#CAVEATS\">CAVEATS<\/a><br \/> <a href=\"#VERSION\">VERSION<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#AUTHOR\">AUTHOR<\/a><br \/> <a href=\"#COPYRIGHT AND LICENSE\">COPYRIGHT AND LICENSE<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">autobox \u2212 call methods on native types<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox; <br \/> # integers <br \/> my $range = 10\u2212>to(1); # [ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ] <br \/> # floats <br \/> my $error = 3.1415927\u2212>minus(22\/7)\u2212>abs(); <br \/> # strings <br \/> my @list = &#8216;SELECT bodies manpages.csv script_extrae_body.sh script.sh usr FROM foo&#8217;\u2212>list(); <br \/> my $greeting = &#8220;Hello, world!&#8221;\u2212>upper(); # &#8220;HELLO, WORLD!&#8221; <br \/> $greeting\u2212>for_each(&#038;character_handler); <br \/> # arrays and array refs <br \/> my $schwartzian = @_\u2212>map(&#8230;)\u2212>sort(&#8230;)\u2212>map(&#8230;); <br \/> my $hash = [ &#8216;SELECT bodies manpages.csv script_extrae_body.sh script.sh usr FROM foo WHERE id IN (?, ?)&#8217;, 1, 2 ]\u2212>hash(); <br \/> # hashes and hash refs <br \/> { alpha => &#8216;beta&#8217;, gamma => &#8216;vlissides&#8217; }\u2212>for_each(&#8230;); <br \/> %hash\u2212>keys(); <br \/> # code refs <br \/> my $plus_five = (&#038;add)\u2212>curry()\u2212>(5); <br \/> my $minus_three = sub { $_[0] \u2212 $_[1] }\u2212>reverse\u2212>curry\u2212>(3); <br \/> # can, isa, VERSION, import and unimport can be accessed via autobox_class <br \/> 42\u2212>autobox_class\u2212>isa(&#8216;MyNumber&#8217;) <br \/> say []\u2212>autobox_class\u2212>VERSION<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">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.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Autoboxing is transparent: values are not blessed into their (user-defined) implementation class (unless the method elects to bestow such a blessing) \u2212 they simply use its methods as though they are.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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 &#8220;ref()&#8221; type of that value \u2212 or <small>SCALAR<\/small> if the value is a non-reference.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This mapping can be overridden by passing key\/value pairs to the &#8220;use autobox&#8221; statement, in which the keys represent native types, and the values their associated classes.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">As with regular objects, autoboxed values are passed as the first argument of the specified method. Consequently, given a vanilla &#8220;use autobox&#8221;:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">&#8220;Hello, world!&#8221;\u2212>upper()<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">is invoked as:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">SCALAR::upper(&#8220;hello, world!&#8221;)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">while:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">[ 1 .. 10 ]\u2212>for_each(sub { &#8230; })<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">resolves to:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">ARRAY::for_each([ 1 .. 10 ], sub { &#8230; })<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Values beginning with the array &#8220;@&#8221; and hash &#8220;%&#8221; sigils are passed by reference, i.e. under the default bindings:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">@array\u2212>join(&#8216;, &#8216;) <br \/> @{ &#8230; }\u2212>length() <br \/> %hash\u2212>keys() <br \/> %$hash\u2212>values()<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">are equivalent to:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">ARRAY::join(@array, &#8216;, &#8216;) <br \/> ARRAY::length(@{ &#8230; }) <br \/> HASH::keys(%hash) <br \/> HASH::values(%$hash)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Multiple &#8220;use autobox&#8221; statements can appear in the same scope. These are merged both &#8220;horizontally&#8221; (i.e. multiple classes can be associated with a particular type) and &#8220;vertically&#8221; (i.e. multiple classes can be associated with multiple types).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Thus:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox SCALAR => &#8216;Foo&#8217;; <br \/> use autobox SCALAR => &#8216;Bar&#8217;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\u2212 associates <small>SCALAR<\/small> types with a synthetic class whose @ISA includes both Foo and Bar (in that order).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Likewise:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox SCALAR => &#8216;Foo&#8217;; <br \/> use autobox SCALAR => &#8216;Bar&#8217;; <br \/> use autobox ARRAY => &#8216;Baz&#8217;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">and<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox SCALAR => [ &#8216;Foo&#8217;, &#8216;Bar&#8217; ]; <br \/> use autobox ARRAY => &#8216;Baz&#8217;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\u2212 bind <small>SCALAR<\/small> types to the Foo and Bar classes and <small>ARRAY<\/small> types to Baz.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">autobox is lexically scoped, and bindings for an outer scope can be extended or countermanded in a nested scope:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">{ <br \/> use autobox; # default bindings: autobox all native types <br \/> &#8230; <br \/> { <br \/> # appends &#8216;MyScalar&#8217; to the @ISA associated with SCALAR types <br \/> use autobox SCALAR => &#8216;MyScalar&#8217;; <br \/> &#8230; <br \/> } <br \/> # back to the default (no MyScalar) <br \/> &#8230; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Autoboxing can be turned off entirely by using the &#8220;no&#8221; syntax:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">{ <br \/> use autobox; <br \/> &#8230; <br \/> no autobox; <br \/> &#8230; <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\u2212 or can be selectively disabled by passing arguments to the &#8220;no autobox&#8221; statement:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox; # default bindings <br \/> no autobox qw(SCALAR); <br \/> []\u2212>foo(); # OK: ARRAY::foo([]) <br \/> &#8220;Hello, world!&#8221;\u2212>bar(); # runtime error<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Autoboxing is not performed for barewords i.e.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $foo = Foo\u2212>new();<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">and:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $foo = new Foo;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">behave as expected.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">sub method1 { &#8230; } <br \/> my $method2 = &#8216;some_method&#8217;; <br \/> my $method3 = sub { &#8230; }; <br \/> my $method4 = &#038;some_method; <br \/> &#8221; &#8230; &#8220;\u2212>method1(); <br \/> [ &#8230; ]\u2212>$method2(); <br \/> { &#8230; }\u2212>$method3(); <br \/> sub { &#8230; }\u2212>$method4();<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">A native type is only associated with a class if the type => class mapping is supplied in the &#8220;use autobox&#8221; statement. Thus the following will not work:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox SCALAR => &#8216;MyScalar&#8217;; <br \/> @array\u2212>some_array_method();<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\u2212 as no class is specified for the <small>ARRAY<\/small> 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:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Can&#8217;t call method &#8220;some_array_method&#8221; on unblessed reference at &#8230;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">As a convenience, there is one exception to this rule. If &#8220;use autobox&#8221; is invoked with no arguments (ignoring the <small>DEBUG<\/small> option) the four main native types are associated with classes of the same name.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Thus:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">\u2212 is equivalent to:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox { <br \/> SCALAR => &#8216;SCALAR&#8217;, <br \/> ARRAY => &#8216;ARRAY&#8217;, <br \/> HASH => &#8216;HASH&#8217;, <br \/> CODE => &#8216;CODE&#8217;, <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This facilitates one-liners and prototypes:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox; <br \/> sub SCALAR::split { [ split &#8221;, $_[0] ] } <br \/> sub ARRAY::length { scalar @{$_[0]} } <br \/> print &#8220;Hello, world!&#8221;\u2212>split\u2212>length();<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">However, using these default bindings is not recommended as there\u2019s no guarantee that another piece of code won\u2019t trample over the same namespace\/methods.<\/p>\n<h2>OPTIONS <a name=\"OPTIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">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 &#8220;use autobox&#8221; statement.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The following example shows the range of valid arguments:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox { <br \/> SCALAR => &#8216;MyScalar&#8217; # class name <br \/> ARRAY => &#8216;MyNamespace::&#8217;, # class prefix (ending in &#8216;::&#8217;) <br \/> HASH => [ &#8216;MyHash&#8217;, &#8216;MyNamespace::&#8217; ], # one or more class names and\/or prefixes <br \/> CODE => &#8230;, # any of the 3 value types above <br \/> INTEGER => &#8230;, # any of the 3 value types above <br \/> FLOAT => &#8230;, # any of the 3 value types above <br \/> NUMBER => &#8230;, # any of the 3 value types above <br \/> STRING => &#8230;, # any of the 3 value types above <br \/> UNDEF => &#8230;, # any of the 3 value types above <br \/> UNIVERSAL => &#8230;, # any of the 3 value types above <br \/> DEFAULT => &#8230;, # any of the 3 value types above <br \/> DEBUG => &#8230; # boolean or coderef <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <small>INTEGER, FLOAT, NUMBER, STRING, SCALAR, ARRAY, HASH, CODE, UNDEF, DEFAULT<\/small> and <small>UNIVERSAL<\/small> options can take three different types of value:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"26%\">\n<p>A class name e.g.<\/p>\n<\/td>\n<td width=\"57%\"> <\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\">use autobox INTEGER => &#8216;MyInt&#8217;;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">This binds the specified native type to the specified class. All methods invoked on values of type &#8220;key&#8221; will be dispatched as methods of the class specified in the corresponding &#8220;value&#8221;.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p style=\"margin-top: 1em\">\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p style=\"margin-top: 1em\">A namespace: this is a class prefix (up to and including the final \u2019::\u2019) to which the specified type name ( <small>INTEGER, FLOAT, STRING<\/small> &#038;c.) will be appended:<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:17%; margin-top: 1em\">Thus:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">use autobox ARRAY => &#8216;Prelude::&#8217;;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">is equivalent to:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">use autobox ARRAY => &#8216;Prelude::ARRAY&#8217;;<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p style=\"margin-top: 1em\">\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"83%\">\n<p style=\"margin-top: 1em\">A reference to an array of class names and\/or namespaces. This associates multiple classes with the specified type.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\"><b><small>DEFAULT<\/small><\/b> <br \/> The &#8220;DEFAULT&#8221; option specifies bindings for any of the four default types ( <small>SCALAR, ARRAY, HASH<\/small> and <small>CODE<\/small> ) not supplied in the &#8220;use autobox&#8221; statement. As with the other options, the &#8220;value&#8221; corresponding to the &#8220;DEFAULT&#8221; &#8220;key&#8221; can be a class name, a namespace, or a reference to an array containing one or more class names and\/or namespaces.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Thus:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox { <br \/> STRING => &#8216;MyString&#8217;, <br \/> DEFAULT => &#8216;MyDefault&#8217;, <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">is equivalent to:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox { <br \/> STRING => &#8216;MyString&#8217;, <br \/> SCALAR => &#8216;MyDefault&#8217;, <br \/> ARRAY => &#8216;MyDefault&#8217;, <br \/> HASH => &#8216;MyDefault&#8217;, <br \/> CODE => &#8216;MyDefault&#8217;, <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Which in turn is equivalent to:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox { <br \/> INTEGER => &#8216;MyDefault&#8217;, <br \/> FLOAT => &#8216;MyDefault&#8217;, <br \/> STRING => [ &#8216;MyString&#8217;, &#8216;MyDefault&#8217; ], <br \/> ARRAY => &#8216;MyDefault&#8217;, <br \/> HASH => &#8216;MyDefault&#8217;, <br \/> CODE => &#8216;MyDefault&#8217;, <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Namespaces in <small>DEFAULT<\/small> values have the default type name appended, which, in the case of defaulted <small>SCALAR<\/small> types, is <small>SCALAR<\/small> rather than <small>INTEGER, FLOAT<\/small> &#038;c.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Thus:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox { <br \/> ARRAY => &#8216;MyArray&#8217;, <br \/> HASH => &#8216;MyHash&#8217;, <br \/> CODE => &#8216;MyCode&#8217;, <br \/> DEFAULT => &#8216;MyNamespace::&#8217;, <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">is equivalent to:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox { <br \/> INTEGER => &#8216;MyNamespace::SCALAR&#8217;, <br \/> FLOAT => &#8216;MyNamespace::SCALAR&#8217;, <br \/> STRING => &#8216;MyNamespace::SCALAR&#8217;, <br \/> ARRAY => &#8216;MyArray&#8217;, <br \/> HASH => &#8216;MyArray&#8217;, <br \/> CODE => &#8216;MyCode&#8217;, <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Any of the four default types can be exempted from defaulting to the <small>DEFAULT<\/small> value by supplying a value of undef:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox { <br \/> HASH => undef, <br \/> DEFAULT => &#8216;MyDefault&#8217;, <br \/> } <br \/> 42\u2212>foo # ok: MyDefault::foo <br \/> []\u2212>bar # ok: MyDefault::bar <br \/> %INC\u2212>baz # not ok: runtime error<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b><small>UNDEF<\/small><\/b> <br \/> The pseudotype, <small>UNDEF,<\/small> can be used to autobox undefined values. These are not autoboxed by default.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This doesn\u2019t work:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox; <br \/> undef\u2212>foo() # runtime error<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This works:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox UNDEF => &#8216;MyUndef&#8217;; <br \/> undef\u2212>foo(); # ok<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">So does this:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox UNDEF => &#8216;MyNamespace::&#8217;; <br \/> undef\u2212>foo(); # ok<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b><small>NUMBER, SCALAR<\/small> and <small>UNIVERSAL<\/small><\/b> <br \/> The virtual types <small>NUMBER, SCALAR<\/small> and <small>UNIVERSAL<\/small> function as macros or shortcuts which create bindings for their subtypes. The type hierarchy is as follows:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">UNIVERSAL \u2212+ <br \/> | <br \/> +\u2212 SCALAR \u2212+ <br \/> | | <br \/> | +\u2212 NUMBER \u2212+ <br \/> | | | <br \/> | | +\u2212 INTEGER <br \/> | | | <br \/> | | +\u2212 FLOAT <br \/> | | <br \/> | +\u2212 STRING <br \/> | <br \/> +\u2212 ARRAY <br \/> | <br \/> +\u2212 HASH <br \/> | <br \/> +\u2212 CODE<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Thus:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox NUMBER => &#8216;MyNumber&#8217;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">is equivalent to:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox { <br \/> INTEGER => &#8216;MyNumber&#8217;, <br \/> FLOAT => &#8216;MyNumber&#8217;, <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">And:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox SCALAR => &#8216;MyScalar&#8217;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">is equivalent to:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox { <br \/> INTEGER => &#8216;MyScalar&#8217;, <br \/> FLOAT => &#8216;MyScalar&#8217;, <br \/> STRING => &#8216;MyScalar&#8217;, <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Virtual types can also be passed to &#8220;unimport&#8221; via the &#8220;no autobox&#8221; syntax. This disables autoboxing for the corresponding subtypes e.g.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">no autobox qw(NUMBER);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">is equivalent to:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">no autobox qw(INTEGER FLOAT);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Virtual type bindings can be mixed with ordinary bindings to provide fine-grained control over inheritance and delegation. For instance:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox { <br \/> INTEGER => &#8216;MyInteger&#8217;, <br \/> NUMBER => &#8216;MyNumber&#8217;, <br \/> SCALAR => &#8216;MyScalar&#8217;, <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">would result in the following bindings:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">42\u2212>foo \u2212> [ MyInteger, MyNumber, MyScalar ] <br \/> 3.1415927\u2212>bar \u2212> [ MyNumber, MyScalar ] <br \/> &#8220;Hello, world!&#8221;\u2212>baz \u2212> [ MyScalar ]<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note that <small>DEFAULT<\/small> bindings take precedence over virtual type bindings i.e.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox { <br \/> UNIVERSAL => &#8216;MyUniversal&#8217;, <br \/> DEFAULT => &#8216;MyDefault&#8217;, # default SCALAR, ARRAY, HASH and CODE before UNIVERSAL <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">is equivalent to:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox { <br \/> INTEGER => [ &#8216;MyDefault&#8217;, &#8216;MyUniversal&#8217; ], <br \/> FLOAT => [ &#8216;MyDefault&#8217;, &#8216;MyUniversal&#8217; ], # &#8230; &#038;c. <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b><small>DEBUG<\/small><\/b> <br \/> &#8220;DEBUG&#8221; allows the autobox bindings for the current scope to be inspected, either by dumping them to the console or passing them to a callback function. This allows the computed bindings to be seen in &#8220;longhand&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The option is ignored if the value corresponding to the &#8220;DEBUG&#8221; key is false.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If the value is a <small>CODE<\/small> ref, it is called with a reference to the hash containing the computed bindings for the current scope.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Finally, if &#8220;DEBUG&#8221; is true but not a <small>CODE<\/small> ref, the bindings are dumped to <small>STDERR.<\/small><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Thus:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox DEBUG => 1, &#8230;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">or<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox DEBUG => sub { &#8230; }, &#8230;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">or<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">sub my_callback ($) { <br \/> my $hashref = shift; <br \/> &#8230; <br \/> } <br \/> use autobox DEBUG => &#038;my_callback, &#8230;<\/p>\n<h2>METHODS <a name=\"METHODS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>import<\/b> <br \/> This method sets up autobox bindings for the current lexical scope. It can be used to implement autobox extensions i.e. lexically-scoped modules that provide autobox bindings for one or more native types without requiring calling code to &#8220;use autobox&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This is done by subclassing autobox and overriding &#8220;import&#8221;. This allows extensions to effectively translate &#8220;use MyModule&#8221; into a bespoke &#8220;use autobox&#8221; call e.g.:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">package String::Trim; <br \/> use base qw(autobox); <br \/> sub import { <br \/> my $class = shift; <br \/> $class\u2212>SUPER::import( <br \/> STRING => &#8216;String::Trim::String&#8217; <br \/> ); <br \/> } <br \/> package String::Trim::String; <br \/> sub trim { <br \/> my $string = shift; <br \/> $string =~ s\/^s+\/\/; <br \/> $string =~ s\/s+$\/\/; <br \/> $string; <br \/> } <br \/> 1;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note that &#8220;trim&#8221; is defined in an auxiliary class rather than in String::Trim itself to prevent String::Trim\u2019s own methods (i.e. the methods it inherits from autobox) being exposed to &#8220;STRING&#8221; types.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This module can now be used without a &#8220;use autobox&#8221; statement to enable the &#8220;trim&#8221; method in the current lexical scope e.g.:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">#!\/usr\/bin\/env perl <br \/> use String::Trim; <br \/> print &#8221; Hello, world! &#8220;\u2212>trim();<\/p>\n<h2>UNIVERSAL METHODS FOR AUTOBOXED TYPES <a name=\"UNIVERSAL METHODS FOR AUTOBOXED TYPES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>autobox_class<\/b> <br \/> autobox adds a single method to all autoboxed types: &#8220;autobox_class&#8221;. This can be used to call <small>UNIVERSAL<\/small> methods i.e. &#8220;can&#8221;, &#8220;DOES&#8221;, &#8220;import&#8221;, &#8220;isa&#8221;, &#8220;unimport&#8221; and &#8220;VERSION&#8221; e.g.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">if (sub { &#8230; }\u2212>autobox_class\u2212>can(&#8216;curry&#8217;)) &#8230; <br \/> if (42\u2212>autobox_class\u2212>isa(&#8216;SCALAR&#8217;)) &#8230;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note: &#8220;autobox_class&#8221; must <b>always<\/b> be used when calling these methods. Calling them directly on native types produces the same results as calling them with autobox disabled e.g.:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">42\u2212>isa(&#8216;NUMBER&#8217;) # &#8220;&#8221; (interpeted as &#8220;42&#8221;\u2212>isa(&#8220;NUMBER&#8221;)) <br \/> []\u2212>can(&#8216;push&#8217;) # Error: Can&#8217;t call method &#8220;can&#8221; on unblessed reference<\/p>\n<h2>EXPORTS <a name=\"EXPORTS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>type<\/b> <br \/> autobox includes an additional module, autobox::universal, which exports a single subroutine, &#8220;type&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This sub returns the type of its argument within autobox (which is essentially longhand for the type names used within perl). This value is used by autobox to associate a method invocant with its designated classes e.g.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox::universal qw(type); <br \/> type(&#8220;42&#8221;) # STRING <br \/> type(42) # INTEGER <br \/> type(42.0) # FLOAT <br \/> type(undef) # UNDEF<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">autobox::universal is loaded automatically by autobox, and, as its name suggests, can be used to install a universal &#8220;type&#8221; method for autoboxed values e.g.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox UNIVERSAL => &#8216;autobox::universal&#8217;; <br \/> 42\u2212>type # INTEGER <br \/> 3.1415927\u2212>type # FLOAT <br \/> %ENV\u2212>type # HASH<\/p>\n<h2>CAVEATS <a name=\"CAVEATS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Performance<\/b> <br \/> Calling<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">&#8220;Hello, world!&#8221;\u2212>length()<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">is slightly slower than the equivalent method call on a string-like object, and significantly slower than<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">length(&#8220;Hello, world!&#8221;)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Gotchas<\/b> <i><br \/> Precedence<\/i><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Due to Perl\u2019s precedence rules, some autoboxed literals may need to be parenthesized:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">For instance, while this works:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $curried = sub { &#8230; }\u2212>curry();<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">this doesn\u2019t:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $curried = &#038;foo\u2212>curry();<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The solution is to wrap the reference in parentheses:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $curried = (&#038;foo)\u2212>curry();<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The same applies for signed integer and float literals:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"># this works <br \/> my $range = 10\u2212>to(1); <br \/> # this doesn&#8217;t work <br \/> my $range = \u221210\u2212>to(10); <br \/> # this works <br \/> my $range = (\u221210)\u2212>to(10);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>print <small>BLOCK<\/small><\/i><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Perl\u2019s special-casing for the &#8220;print BLOCK &#8230;&#8221; syntax (see perlsub) means that &#8220;print { expression() } &#8230;&#8221; (where the curly brackets denote an anonymous <small>HASH<\/small> ref) may require some further disambiguation:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"># this works <br \/> print { foo => &#8216;bar&#8217; }\u2212>foo(); <br \/> # and this <br \/> print { &#8216;foo&#8217;, &#8216;bar&#8217; }\u2212>foo(); <br \/> # and even this <br \/> print { &#8216;foo&#8217;, &#8216;bar&#8217;, @_ }\u2212>foo(); <br \/> # but this doesn&#8217;t <br \/> print { @_ }\u2212>foo() ? 1 : 0<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In the latter case, the solution is to supply something other than a <small>HASH<\/small> ref literal as the first argument to &#8220;print()&#8221;:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"># e.g. <br \/> print STDOUT { @_ }\u2212>foo() ? 1 : 0; <br \/> # or <br \/> my $hashref = { @_ }; <br \/> print $hashref\u2212>foo() ? 1 : 0; <br \/> # or <br \/> print &#8221;, { @_ }\u2212>foo() ? 1 : 0; <br \/> # or <br \/> print &#8221; . { @_ }\u2212>foo() ? 1 : 0; <br \/> # or even <br \/> { @_ }\u2212>print_if_foo(1, 0);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>eval <small>EXPR<\/small><\/i><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Like most pragmas, autobox performs operations at compile time, and, as a result, runtime string &#8220;eval&#8221;s are not executed within its scope i.e. this doesn\u2019t work:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox; <br \/> eval &#8220;42\u2212>foo&#8221;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The workaround is to use autobox within the &#8220;eval&#8221; e.g.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">eval <<'EOS'; <br \/> use autobox; <br \/> 42\u2212>foo(); <br \/> EOS<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note that the &#8220;eval BLOCK&#8221; form works as expected:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use autobox; <br \/> eval { 42\u2212>foo() }; # OK<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>Operator Overloading<\/i><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Operator overloading via the overload pragma doesn\u2019t (automatically) work. autobox works by lexically overriding the arrow operator. It doesn\u2019t bless native types into objects, so overloading \u2212 or any other kind of &#8220;magic&#8221; which depends on values being blessed \u2212 doesn\u2019t apply.<\/p>\n<h2>VERSION <a name=\"VERSION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">3.0.1<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p style=\"margin-top: 1em\">\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"28%\">\n<p style=\"margin-top: 1em\">autobox::Core<\/p>\n<\/td>\n<td width=\"55%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"28%\">\n<p>Moose::Autobox<\/p>\n<\/td>\n<td width=\"55%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"28%\">\n<p>perl5i<\/p>\n<\/td>\n<td width=\"55%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"1%\">\n<p>\u2022<\/p>\n<\/td>\n<td width=\"5%\"><\/td>\n<td width=\"28%\">\n<p>Scalar::Properties<\/p>\n<\/td>\n<td width=\"55%\"> <\/td>\n<\/tr>\n<\/table>\n<h2>AUTHOR <a name=\"AUTHOR\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">chocolateboy <chocolate@cpan.org><\/p>\n<h2>COPYRIGHT AND LICENSE <a name=\"COPYRIGHT AND LICENSE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Copyright (c) 2003\u22122018 by chocolateboy.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This library is free software; you can redistribute it and\/or modify it under the terms of the Artistic License 2.0 <http:\/\/www.opensource.org\/licenses\/artistic-license-2.0.php>.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  autobox \u2212 call methods on native types <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3379,3007],"class_list":["post-7030","post","type-post","status-publish","format-standard","hentry","category-sin-categoria","tag-autobox","tag-man3"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7030","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=7030"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7030\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=7030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=7030"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=7030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}