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


In reply to Autoboxing ... "Yes We Can" ( or how I learned to love TIMTOWTDI ;) by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.