in reply to Hash of subroutines as member of a class

Hello Hameed,

As you don’t provide any example usage, I’m in the dark as to what this code is designed to accomplish. Apparently tangent and GrandFather can see the point to this approach, so there must be one; but...

It seems to me that you’ve come up with a complex and fragile way of simulating inheritance; and one which also has the disadvantage that each object duplicates the same redundant information. For example:

bless({ ACT => "Purchase", ACTIVITIES => { Purchase => { datefield => "date_bought", func => "total_bought" } +, Sale => { datefield => "date_sold", func => "total_sold" }, }, }, "EOYStats")

Consider as an alternative the following skeleton code, which uses simple inheritance:

#! perl use strict; use warnings; #--------------------------------------------------------------------- +--------- package EOYStats; #--------------------------------------------------------------------- +--------- sub new { my ($class) = @_; my $this = bless {}, $class; use Data::Dump; dd $this; return $this; } sub do_something { my ($this, $arg) = @_; print "EOYStats::do_something($arg)\n"; } #--------------------------------------------------------------------- +--------- package EOYStats::Sale; #--------------------------------------------------------------------- +--------- use parent -norequire, 'EOYStats'; sub get_stats { my ($this, $arg) = @_; print "EOYStats::Sale::get_stats()\n"; $this->do_something($arg); } #--------------------------------------------------------------------- +--------- package EOYStats::Purchase; #--------------------------------------------------------------------- +--------- use parent -norequire, 'EOYStats'; sub get_stats { my ($this, $arg) = @_; print "EOYStats::Purchase::get_stats($arg)\n"; $this->do_something($arg); } #--------------------------------------------------------------------- +--------- package main; #--------------------------------------------------------------------- +--------- my $obj = EOYStats::Purchase->new(); $obj->get_stats('abc');

Output:

12:59 >perl 1034_SoPW.pl bless({}, "EOYStats::Purchase") EOYStats::Purchase::get_stats(abc) EOYStats::do_something(abc) 13:09 >

Would this approach work for you? If so, I think you’ll find it simpler and more robust. And if not, an explanation of where it falls short will help to clarify the problem.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Hash of subroutines as member of a class
by Hameed (Acolyte) on Sep 30, 2014 at 03:47 UTC
    Hi Athanasius,

    I had to quickly whip up a few lines to avoid posting the original script that I was working on. I would have had to cut down so much and then it would be more nonsensical than this one.

    I was trying to call a method, and it failed to pass the object. That whole point of the question was to find out what was wrong with that call. tangentwas able to answer that. Thanks to him.

    You and GrandFather suggested different ways of doing that. Much appreciated.

    I am sure your suggestion, being proper OO is much better.

    Thank you. :)