neniro has asked for the wisdom of the Perl Monks concerning the following question:
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:
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?#!/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;
neniro
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Functions to Methods
by Aragorn (Curate) on Jun 14, 2004 at 13:32 UTC | |
by neniro (Priest) on Jun 14, 2004 at 13:41 UTC | |
|
Re: Functions to Methods
by dragonchild (Archbishop) on Jun 14, 2004 at 12:57 UTC | |
|
Re: Functions to Methods
by blueAdept (Beadle) on Jun 14, 2004 at 15:21 UTC | |
by hv (Prior) on Jun 14, 2004 at 18:23 UTC | |
by dragonchild (Archbishop) on Jun 14, 2004 at 18:37 UTC | |
|
Re: Functions to Methods
by bibo (Pilgrim) on Jun 14, 2004 at 16:21 UTC |