Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Functions to Methods

by neniro (Priest)
on Jun 14, 2004 at 12:55 UTC ( [id://366524]=perlquestion: print w/replies, xml ) Need Help??

neniro has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Functions to Methods
by Aragorn (Curate) on Jun 14, 2004 at 13:32 UTC
    I can use functions to work with them. If I'd like to treat them like objects I've to change them into objects
    ... push @$obj, qw ( 12 19 35 37 48 98 25 ); ...
    Especially to handle the object as reference to the Array looks odd (of course it's nothing else than an blessed arrayref).
    First, you want to use an array as an object. You define an array class. Then you use the 'functional' interface again, which makes it ugly. Why not define a push method and call that?
    $obj->push(qw ( 12 19 35 37 48 98 25 ));
    The way you're doing it now is a bit halfhearted.

    Arjen

      The way you're doing it now is a bit halfhearted.

      You're right. I haven't thought it to this point. I've to wrap all possible functions to methods. I've only seen the first step and missed the whole picture.

      Thank you,
      neniro

Re: Functions to Methods
by dragonchild (Archbishop) on Jun 14, 2004 at 12:57 UTC
    Looks like you found the right solution, to me. Personally, I would have an add_values() subroutine that is a wrapper around push, to have encapsulation, but that's a matter of taste.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: Functions to Methods
by blueAdept (Beadle) on Jun 14, 2004 at 15:21 UTC
    You can program in COBOL(style wise) using java, but you probably shouldn't do so. Likewise making an array into an object seems more like an excercise of mimicing that other language rather than providing much real benefit.

    Perl provides enough facilities for managing arrays that its not necessary to objectify them, other than to be a purist :-) At least I can't think of any methods that are lacking/missing. By facilities I mean -1 is the last element, scalar(@array) is the length/number of elements, $#array is the number of the last element. push/pop/unshift/shift - all for adding or removing from the front or the end. The common perl idiom is to use a HASH as your blessed object, and that hash may contain references to simpler types like an arrays. Not critcizing you for doing this..I've done fun things like this to see if I 'could' myself. I'm actually curious as to the reasons you want to objectify an array.
      Perl provides enough facilities for managing arrays that its not necessary to objectify them, other than to be a purist :-)

      To me, the main value of an Array class would be the potential to overload it: that would let me get something just like an array except for one small difference (or maybe except for a big difference).

      perl6 will in large part scratch that itch for me by having classes for all the builtin datatypes, though there will be some restrictions.

      At heart, though, I am a purist - I'd love to see a full "objects everywhere" a la Smalltalk, with a similarly well thought out set of fundamental classes (Bag, Set, Collection etc) - most of the time. The rest of the time I want the raw speed that handcoded builtins can offer. And sometimes both at the same time. And a pony.

      Hugo

        Have you tried tie'ing?

        ------
        We are the carpenters and bricklayers of the Information Age.

        Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

        I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: Functions to Methods
by bibo (Pilgrim) on Jun 14, 2004 at 16:21 UTC
    If you want to get into the groove in a slightly way, try playing with Class::Struct

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://366524]
Approved by Happy-the-monk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-03-28 22:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found