17 June 2007
This post may be outdated due to it was written on 2007. The links may be broken. The code may be not working anymore. Leave comments if needed.
when we use Catalyst::Plugin::Authentication::Store::DBIC::User, we will have $c->user and $c->user->obj. but most of people are confusing with them I guess.

differences:
* $c->user is ISA Catalyst::Plugin::Authentication::Store::DBIC::User, while $c->user->obj is just something like sub obj in Catalyst::Plugin::Authentication::Store::DBIC::User. see:
__PACKAGE__->mk_accessors(qw/id config obj store/);
# .skip
bless {
id => $id,
config => $config,
obj => $user_obj,
}, $class;

* why $c->user->username is the same as $c->user->obj->username is because of
sub AUTOLOAD {
my $self = shift;
(my $method) = (our $AUTOLOAD =~ /([^:]+)$/);
return if $method eq "DESTROY";

$self->obj->$method(@_);
}
. when we call ->username of $c->user. it will call sub username in Catalyst::Plugin::Authentication::Store::DBIC::User. then sub AUTOLOAD works here, it call $self->obj->$method(@_) indeed. ($self is $c->user).

so there is something interesting..
* because we have sub for_session in Catalyst::Plugin::Authentication::Store::DBIC::User, we will have $c->user->for_session ( but not $c->user->obj->for_session if user table don't have "for_session" column ).
* when I see something "*user = \&obj;" in the code. I know I can use $c->user->user->username. it's the same as $c->user->obj->username. (JOKE!)

for most situation. I suggest use $c->user->obj->column_name because that's more direct (no AUTOLOAD involved).

Enjoy!.


blog comments powered by Disqus