Re: Magic for object constructor wanted



On Wed, 30 Jan 2008 22:05:45 -0800, Koszalek Opalek wrote:
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?

Possible, yes. Ugly, too. What you want is a sure-fire sign of a bad
design. I'm only going to show how to do it as an academic exercise to
demonstrate that Perl makes even bad ideas possible. I'm not going to
answer any questions about it. I think it would be a hideous mistake to
put it in production code.

$ cat /tmp/foo
#!/usr/local/bin/perl
use strict;
use warnings;

use PadWalker qw(peek_my);
use List::Util qw(first);

my $my_name = 42;

get_name(\$my_name);

sub get_name {
my $ref = shift;

my $h = peek_my(1);
my $name = first { ref $ref && $h->{$_} eq $ref } keys %$h;
print "Object variable name = ", ($name || "<???>"), "\n";
}

$ /tmp/foo
Object variable name = $my_name

--
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/

.



Relevant Pages

  • Re: ONS - loyal or not?
    ... Our needlecraft shops surely in most cases don't have what I need, ... We have wonderful needlecraft stores online. ... more of our shopping online. ... Design page http://www.KarenMCampbell.com/designs.html ...
    (rec.crafts.textiles.needlework)
  • Re: error message that says too many fields
    ... Another that stores information about Companies. ... A flat design very often leads to duplicate data in your tables. ... For most payments, only a few of those fields ... If the receipt only pays rent, you'll have a record in each table. ...
    (microsoft.public.access.gettingstarted)
  • modelling a contact database
    ... tblCompanies (stores companies' main addresses) ... ChildID (stores RecordID of their employee) ... tblTypes contains three records: Home/Personal, Work, Other. ... I feel like I'm on the right track though, that this table design is smarter ...
    (microsoft.public.access.tablesdbdesign)
  • Re: compare
    ... of these stores, this is really not a good design. ... (Pkey partNumber, Store) ... > is there anyway I could create a colmn called lowest pick the lowest price ...
    (microsoft.public.sqlserver.programming)
  • Re: Halo 3 UK street date broken (seriously)
    ... select few stores (including Shipley and Leeds Crown Point Retail Park if ... you're anywhere near me) are showing as in stock and ready to collect now. ... Reserve online and pick up in store. ... Web Design | Logo Design | Corporate Identity Design ...
    (uk.games.video.xbox)