I just applied the autobox patch and started playing with it. Autoboxing is controversial, but it's rather interesting. Autoboxing is used in Java, C# and Ruby. It allows you to call methods on built-in datatypes, including literals!

The patch is small, but it does require you to compile Perl from source (sorry, folks!). You can read the documentation for more information, but basically, you create a class for your data type and then you can call methods. For example, let's say you want to implement this:

use autobox; my @nums = 1->to(10); my @reverse = 10->to(1);

One way of doing that would be to create the following SCALAR class:

package SCALAR; + + sub to { my ($from, $to) = @_; return $from > $to ? (reverse $to .. $from) : ($from .. $to); } 1;

It looks nifty, but it seems to have some annoying limitations:

  @nums->sort;

That looks good, but you might expect that you would create this in the ARRAY package (as you would with array refs). In this case, however, you get the error:

Can't locate object method "sort" via package "SCALAR" at ./test.pl line 30.

Having an array call methods in a scalar class is not intuitive.

Finally, I spent a couple of minutes implementing a Schartzian Transform:

my @data = ( 'test12-05-2003this', 'test11-05-2003this', 'test12-06-2003this', 'test12-05-2000this', ); @data = schwartzian(@data); print Dumper \@data; + + sub schwartzian { # do I *really* have to use strings here? :( my $result = [@_] ->map ('[$_,main::get_date($_)]') ->sort('$a->[1] <=> $b->[1]') ->map ('$_->[0]'); return @$result; } + + sub get_date { my ($month,$day,$year) = shift =~ /(\d+)-(\d+)-(\d+)/; return "$year$month$day"; }

And the supporting package:

package ARRAY; + + sub grep { [eval "grep {$_[1]} \@{\$_[0]}"] } sub map { [eval "map {$_[1]} \@{\$_[0]}"] } sub sort { [eval "sort {$_[1]} \@{\$_[0]}"] } + + # reverse is not used in this example sub reverse { [reverse @{$_[0]} ] } 1;

I'm not sure how to clean up the Schwartzian Transform with this code, so please take a swing at it. Passing strings of code is ugly.

Autoboxing seems kind of nifty, but I'm not sure if it gains enough to be worthwhile (though I thought it was cool at first). If you haven't already, read through the "controversial" thread above and then let me know what you think. Do you like autoboxing or not? What would you use it for, if anything. Code samples would be great.

Cheers,
Ovid

New address of my CGI Course.


In reply to Autoboxing: Yes or No? by Ovid

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.