Dear fellow monks,
I started an effort to get into OOP with Perl. Changing functions to methods is a point I'm in trouble with. If we look to Ruby an array is an object with methods. In Perl an array is an array. I can use functions to work with them. If I'd like to treat them like objects I've to change them into objects:
#!/usr/bin/perl
package enh_Array;
sub new {
my $class = shift;
$class = ref($class) || $class;
my $self = [];
bless($self, $class);
return $self;
}
sub selected (&) {
my $self = shift;
my $func = shift;
return grep &{$func}, @$self;
}
#
# End of package
#
package main;
use strict;
use warnings;
use Data::Dumper;
my $obj = new enh_Array;
push @$obj, qw ( 12 19 35 37 48 98 25 );
print Dumper [$obj->selected(sub {($_ > 35)})];
exit;
This works fine but it looks ugly to me. Especially to handle the object as reference to the Array looks odd (of course it's nothing else than an blessed arrayref). Is there a smarter way way to do this?
neniro