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,


In reply to Re: Hash of subroutines as member of a class by Athanasius
in thread Hash of subroutines as member of a class by Hameed

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.