perl-diddler has asked for the wisdom of the Perl Monks concerning the following question:
I have (what I thought) was a fairly straight forward set of classes that are all subclasses of the one above (no multiple inheritance issues at this point).
I'm storing data object data in a blessed hash that's created in the most 'base' class's 'new' routine. That's blessed in the base class and returned 'up' the call stack to the successive, derived-class, 'new' routines, where they may add their own data (as more hash entries in the same hash). They then bless and return the has w/any new entries to the next class in line.
So classes 'URL' => URL::Storable => URL::Fetchable are subclasses in that order from least specific to 'derived'.
In URL, code looks like (abbreviated):
in derived classes, the 'new' looks like:package URL; {... sub new { my $package=shift; my $argsp = $_[0]; my $up= { host => '', path => '', }; foreach (keys %$up, @arg_aliases) { $up->{$compose_argmap{_tl( $_)}}=$argsp->{$_} if exists $argsp-> +{$_}; } bless $up, $package; } }
And so on... If the above is the 1st derived class, then on the 2nd derived class (derived from the 1st derived class, not the base class of the 1st), gets an error when I try to bless it:package URL::Storable; { our @ISA=qw(URL); sub new { my $package=shift; my $argsp = $_[0]; # setting a "per-class" var, not per-instance if (exists $argsp->{save_prefix}) { $save_prefix=$argsp->{save_prefix}; } my $this=new URL(@_); # (process this packages args....) bless $this, $package; #bless and return } }
Attempt to bless into a reference at filename.pl line xxx.
Is what I am doing, reasonable for a smallish project (at this point, its all in 1 file). If it is, then is there common 'gotcha', that would cause this?
Isn't this 'a' normal way of doing classing with each level plugging in it's local-object data, then blessing it at the current class level, and returning it. Then any call on an object would (in theory), go from the most derived object look in it's "ISA" for the next, and go to the next level?
If this all seems straightforward so far, and nothing looks amiss, I can start more wholesale copying of code into this question, but I didn't want to go into unnecessary detail for something that seems pretty straight forward, where I'm feeling I must be overlooking something obvious OR have some basic misunderstanding about how things work somewhere, but since the code 'look's fairly simple, I'm checking my understanding first, since I'm not sure why, for example, it would fail when I bless in the 2nd level derivation class, as opposed to in the first-level derivation class, where I'm first blessing that returned 'object' from the base class, URL, after having added an arg to it.
Ideas or 'clue-sticks' welcomed...
thanks...
(Note, fixed example typo resulting from starting with a demo case, then copying in actual code (re: $up (in code for url pointer), vs. $url used in demo case).
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: blessed confusion
by jethro (Monsignor) on Sep 08, 2010 at 23:53 UTC | |
by perl-diddler (Chaplain) on Sep 09, 2010 at 03:35 UTC | |
by ikegami (Patriarch) on Sep 09, 2010 at 05:19 UTC | |
Re: blessed confusion
by chromatic (Archbishop) on Sep 08, 2010 at 23:51 UTC | |
by perl-diddler (Chaplain) on Sep 09, 2010 at 02:42 UTC | |
by chromatic (Archbishop) on Sep 09, 2010 at 06:06 UTC | |
by ikegami (Patriarch) on Sep 09, 2010 at 02:51 UTC | |
Re: blessed confusion
by GrandFather (Saint) on Sep 09, 2010 at 00:00 UTC | |
Re: blessed confusion
by bluescreen (Friar) on Sep 08, 2010 at 23:47 UTC | |
by perl-diddler (Chaplain) on Sep 09, 2010 at 00:34 UTC | |
Re: blessed confusion
by ikegami (Patriarch) on Sep 09, 2010 at 02:58 UTC | |
by perl-diddler (Chaplain) on Sep 09, 2010 at 03:53 UTC | |
by ikegami (Patriarch) on Sep 09, 2010 at 05:10 UTC | |
by perl-diddler (Chaplain) on Sep 09, 2010 at 21:35 UTC | |
by ikegami (Patriarch) on Sep 09, 2010 at 23:14 UTC |