http://www.perlmonks.org?node_id=1067053

Perl is like one of these old text adventures, you keep collecting things from perldoc and suddenly you can open secret doors ...

There has always been much discussion (well flames) about a language idiom called "autoboxing"² missing in Perl.

I just stumbled over a way of emulating it in Perl by using "method" references.

The idiom

Write an anonymous sub like a method, i.e. take $_[0] as $self.

Then call thing->$method_ref(args) and thing doesn't need to be blessed.

DB<158> %h=(); DB<159> ref $h{a}{b}[2]{c} # not an object => "" DB<160> $cycle = sub { $_[0]++; $_[0] %= $_[1] } DB<161> $h{a}{b}[2]{c}->$cycle(3); \%h => { a => { b => [undef, undef, { c => 1 }] } } DB<162> $h{a}{b}[2]{c}->$cycle(3); \%h => { a => { b => [undef, undef, { c => 2 }] } } DB<163> $h{a}{b}[2]{c}->$cycle(3); \%h => { a => { b => [undef, undef, { c => 0 }] } }

The documentation
See perlop:

The Arrow Operator

...

Otherwise, the right side is a method name or a simple scalar variable containing either the method name or a subroutine reference, and the left side must be either an object (a blessed reference) or a class name (that is, a package name). See perlobj.

I used this technique in the past to either

IIRC it's explained in detail somewhere in the "perloo*" group of perldocs...

The reasoning

See wikipedia, in short autoboxing allows to use a method call syntax on "things" which aren't blessed objects, like

DB<100> $a=5 => 5 DB<101> $a->inc Can't call method "inc" without a package or object reference

The general idea is to combine the flexibility of method call syntax, avoiding the overhead to create a real object.

For instance a (contrived) example is JS which has the primitive type "string" and the class¹ "String". This allows to write

'literal string'.match(/string/)

One way Perl could profit from such a syntax are nested data structures, where often manipulations result in ugly unDRYness and lots of nested punctuation.

A simple example: ¹

push @newelements, @{ $a{blossom}{caterpillar}{$loopvalue}[CONSTANT] +{Iforgot} }

in JS you can not only avoid the sigil, you can also _chain_ method calls

a["blossom"]["caterpillar"][loopvalue][CONSTANT]["Iforgot"].push(ele +ments).shift()

The alternatives

There are more reasons to like autoboxing, there were attempts to overload -> to allow this in autobox but this is considered to be a too fragile hack.

Attempts to include this syntax as a language feature were regularly rejected / ignored / postponed.

IIRC does Perl6 intend to have it right away from the start.

And now ->$RFC

I was urged in the CB to write a meditation ASAP, so pardon me for typos, broken links and untested code³ which will need updates.

Being aware of fundamentalists hating this coding style I'm now counting the seconds till the outrage starts... =)

Others may want to add some other use cases ...

Cheers Rolf

( addicted to the Perl Programming Language)

Footnotes

1) yes I know that push was extended "recently" to allow $arr_refs but 5.10 is still relevant

²) aka "boxing" aka "wrapper-classes" see also: wikipedia

³) and euphoria induced by coffee abuse