Jeff 'japhy' Pinyan wrote:
On Jul 13, Scott R. Godin said:

The original object itself is at http://www.webdragon.net/miscel/Subscriber.pm

Now updated some, as mentioned below :)

(the Subscriber::DB object will need $database, $username, $password, a DBI $dbh handle, and possibly a $hostname as well, in addition to the data stored in the Subscriber object, that I wish to make persistent by storing and retrieving it from the mysql database. )


The first thing you need to do is figure out the mapping from the old methods to the new methods.

I'm not quite certain what you're getting at, here. You mean, which methods will get re-mapped to do things slightly differently for the DB version?

One gripe I have with Subscriber.pm is that you can't set fields after the new() method, without knowing the construction of the object. It'd be nice to have a set_XXX() method which set the field to a given value. You could do this with AUTOLOAD and it'd be magic:

True, I'd been meaning to get around to that, but hadn't quite. So, just for you, I did. The weblink above has the current version.

  AUTOLOAD {
    my $obj = shift;
    (my $method = $AUTOLOAD) =~ s/.*:://;
    if ($method =~ /^set(_\w+)/) {
      $obj->{$1} = shift;
    }
    else {
      die "no such method '$method'"
    }
  }

Although I did it slightly differently than this :)

Then you could do

  my $who = Subscriber->new;
  $who->set_zipcode('08536');

Or you could have a set() method:

  sub set {
    my $self = shift;

    while (@_) {
      my ($field, $value) = @_;
      if (exists $self->{$field}) { $self->{$field} = $value }
      else { die "no such field '$field'" }
    }
  }

I also added an $obj->set() method (again slightly different from the above so you'd get warnings about unbalanced pairs) for handling multiple field/value pairs which again checks to make sure the fields are allowed and accessible. I'm pondering whether I should be doing the same thing twice (in new() and set()), and whether it makes any sense to have new call (a slightly different variant of) set.

Maybe you'd want to warn() there instead of die(), I don't know. The point is, then you could write:

  my $person = Subscriber->new;
  $person->set(
    _firstname => "Jeff",
    _lastname => "Pinyan",
  );

yup, makes sense.

[snip]

If you want to hear more, I can give you more. Let me know if this has gone over your head though. I have a system in mind that would greatly reduce the amount of code in each of Subscriber::DB's methods that link to Subscriber's methods.

I'd definitely like to discuss this further.. Great response, Jeff. I really appreciate it! What I'll do is address different portions of your response in different replies so we can have separate sub-threads on smaller bits of the topic, so I can keep it all straight in my head :)

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to