Re: Magic for object constructor wanted



On Jan 31, 8:39 am, Martijn Lievaart <m...@xxxxxxxxxxxxxxxx> wrote:

Sure. Just don't put the objects in separate variables, put them in a
hash.

Even better, bless the hash, iow make it its own class.

Then you get something like:

use Friends;

I'm afraid that misses the point (or I have misunderstood
you). I do not want to bless my objects into Friends.
I want to bless each one into a signle Dude so that I
can do things like

$joe->whoami()

(And I do not want to rewrite code, that takes it for
granted that $joe, $tim, and $ann have all been blessed
into Dude's.)


What you also can do, is store the name of the Dude in the Dude object.
This is better design anyhow, but does not save you the extra redundancy
of specifying the name twice:

my $joe = Dude->new("joe");
my $tom = Dude->new("tom");
my $ann = Dude->new("ann");
my $jane = Dude->new("jane");


That is not different from what I originally
posted except that (name=>"joe") has been
moved from fill() to new().

Koszalek
.



Relevant Pages

  • Re: blessing a hash
    ... yeah that true,but theres little change in the way i build the hash,I build the hash from variables in the text file. ... You have two choices: use a hash reference from the start or take a reference of the hash at the time you call bless: ... my $class = shift; ...
    (perl.beginners)
  • Re: Using lock_keys with strict and bless
    ... > You seem to be confused about how Perl modules work. ... > $this as a hash, because it isn't a hash reference - or any reference ... Your bless() statement blesses an anonymous hash into ...
    (comp.lang.perl.misc)
  • Multiple Inheritance: mixed base class refs (hash, array)
    ... I've written (an event handling class, that uses the moral HASH style ... class container, for use with bless.) ... I noticed that IO:Select actually uses an ARRAY ref, ...
    (comp.lang.perl.misc)
  • Re: Magic for object constructor wanted
    ... Even better, bless the hash, iow make it its own class. ... not want to bless my objects into Friends. ... It's better design, that's what different. ...
    (comp.lang.perl.misc)
  • Re: print_r in Perl
    ... I'm asking whether I can have something higher-level that works for objects on the abstraction level where one usually looks at an object. ... because it would say HASH instead of Some::Package. ... p is $VAR1 = bless([ ...
    (perl.beginners)