Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

(z) SUPER and Class::DBI

by zigdon (Deacon)
on Dec 30, 2003 at 21:42 UTC ( [id://317785]=perlquestion: print w/replies, xml ) Need Help??

zigdon has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I thought I understood how SUPER works - but today I tried to override a method in a CDBI object, and things didn't happen as I expected.

I have a My::User.pm, which (among other columns), has a Password column. Since I'm hashing the password, I wanted to able to call $u->password("secret"), and have it stored in the database automatically hashed. So in my User.pm module, I tried to override the password method:

sub password { my $self = shift; if (@_) { # we're setting a value $_[0] = Digest::MD5::md5_base64($_[0]); } return $self->SUPER::password(@_); }

But when I try this, I get the following error:

Can't locate object method "password" via package "My::User" at My/Use +r.pm line 37.

So I have two questions:

  1. What am I doing wrong with the SUPER call?
  2. I'm sure this can be done with triggers - is "before_set_password" the only one I need to set?

Thanks!

-- zigdon

Replies are listed 'Best First'.
Re: (z) SUPER and Class::DBI
by edoc (Chaplain) on Dec 30, 2003 at 22:10 UTC

    mm.. can't find the reference I've read but basically..

    In CDBI your column accessors are dynamically created, not inherited from another class so when you override a method, then it's gone and there is no SUPER::method.

    In this instance you probably want to use the basic set method:

    # ... return $self->set(password => $_[0]);

    cheers,

    J

Re: (z) SUPER and Class::DBI
by duct_tape (Hermit) on Dec 30, 2003 at 22:27 UTC

    I believe that SUPER:: isn't going to work because that would be trying to call the password method from whatever your class is inheriting from. ie: Class::DBI. (not 100% positive on that). Since the password method is going to exist in your current package, you are overwriting it and SUPER:: will not work.

    CDBI uses Class::Accessor to handle the accessor/mutator creation. When it creates an accessor it creates an alias to it called _WHATEVER_accessor. So you can do this instead:

    return $self->_password_accessor(@_)

    However that this will not work when using create or find_or_create. Only for modification. I would go for the trigger option instead. If you are going to use triggers you can use before_create and before_set_password.

    The one other method you can do is create a class for the passwords, and then set a has_a relationship for the password column. Since this is a fairly simple task, that may be overkill. But it is something to think about.

    -Brad
Re: (z) SUPER and Class::DBI
by PodMaster (Abbot) on Dec 31, 2003 at 07:08 UTC
      That is exactly it! Thanks! For reference, here's what I did, and now everything works as a charm:
      sub normalize_column_values { my $self = shift; my $hashref = shift; if (exists $hashref->{password}) { $hashref->{password} = Digest::MD5::md5_base64($hashref->{password +}); } }
      Thanks!

      -- zigdon

Re: (z) SUPER and Class::DBI
by sgifford (Prior) on Dec 30, 2003 at 21:50 UTC

    What you've got looks OK to me on its own. Perhaps it's something else in your class?

    What's in your @ISA array? Is your package declaration right? Is your reference blessed correctly? Can you post more of your module, especially the declarations that make it work like an object?

      Here's the relevatn declarations:

      package My::User; use base 'My::DBI'; Library::User->table('Users'); Library::User->columns(All => qw/UserID Email Password/);
      And that's really the only part that matters? I don't have any custom constructors, so really, CDBI is taking care of the ISA, blessing, etc.

      Thanks for your suggestions!

      -- zigdon

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://317785]
Approved by sgifford
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-26 00:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found