Magic for object constructor wanted
- From: Koszalek Opalek <koszalekopalek@xxxxxxxxxx>
- Date: Wed, 30 Jan 2008 22:05:45 -0800 (PST)
My code creates new objects and then populates them with
data, like this:
my $joe = Dude->new();
my $tom = Dude->new();
my $ann = Dude->new();
my $jane = Dude->new();
$joe->fill(
name => "joe",
friends => [ $ann, $tom ]
);
$jane->fill(
name => "jane",
friends => [ $ann ]
);
You will notice that the name field is always the same as
the variable that stores the object. What kind of magic
would I have to use in the constructor (or in the fill()
method) so that the name of the object does not have
to be specified explicitly? In other words, I would like to
be able to say:
$joe->fill(
friends => [ $ann, tom ]
);
$jane->fill(
friends => [ $ann ]
);
and still have the name field set. This saves typing and
makes sure the variable name and the name field match.
Is this possible?
Koszalek
.
- Follow-Ups:
- Re: Magic for object constructor wanted
- From: Peter Scott
- Re: Magic for object constructor wanted
- From: bugbear
- Re: Magic for object constructor wanted
- From: Martijn Lievaart
- Re: Magic for object constructor wanted
- From: Koszalek Opalek
- Re: Magic for object constructor wanted
- Prev by Date: Re: perl problem
- Next by Date: Re: Magic for object constructor wanted
- Previous by thread: perl problem
- Next by thread: Re: Magic for object constructor wanted
- Index(es):