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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Autoboxing: Yes or No?
by BrowserUk (Patriarch) on Jan 01, 2004 at 01:06 UTC | |
|
Re: Autoboxing: Yes or No?
by Anonymous Monk on Jan 01, 2004 at 01:10 UTC | |
by tilly (Archbishop) on Jan 01, 2004 at 03:35 UTC | |
|
Re: Autoboxing: Yes or No?
by Corion (Patriarch) on Jan 01, 2004 at 17:08 UTC | |
|
Re: Autoboxing: Yes or No?
by Abigail-II (Bishop) on Jan 01, 2004 at 13:40 UTC | |
|
Re: Autoboxing: Yes or No?
by oha (Friar) on Jan 01, 2004 at 18:32 UTC | |
by Ovid (Cardinal) on Jan 01, 2004 at 19:09 UTC | |
by mirod (Canon) on Jan 01, 2004 at 20:05 UTC | |
by chromatic (Archbishop) on Jan 01, 2004 at 20:52 UTC | |
by mirod (Canon) on Jan 01, 2004 at 21:35 UTC | |
| |
by Aristotle (Chancellor) on Jan 01, 2004 at 23:43 UTC | |
by Ovid (Cardinal) on Jan 02, 2004 at 00:24 UTC | |
| |
by mirod (Canon) on Jan 02, 2004 at 01:57 UTC | |
by BUU (Prior) on Jan 01, 2004 at 20:17 UTC | |
by mirod (Canon) on Jan 01, 2004 at 20:22 UTC | |
by bsb (Priest) on Jan 02, 2004 at 01:21 UTC | |
by Aristotle (Chancellor) on Jan 02, 2004 at 03:07 UTC | |
|