[Catalyst] Re: Accessing $c from Model

Daniel McBrearty danielmcbrearty at gmail.com
Fri Dec 29 19:47:39 GMT 2006


FWIW ... : what I've noticed about using models (or not) ... :

1. the advantage of using a model mostly seems to be that it
autoloads, and then is accessible everywhere from $c. Otherwise, there
doesn't seem to be much difference from just having a normal perl
library.

2. so if you just need data/logic for use in one controller, a
standard library may be better.

3. usually, I'm finding its better to try to keep the models as pure
data sources, and not have them interact, or be context dependent. So
I try to make it the controllers job to do anything that involves
context, or to connect different models together where necessary. That
seems to result in a cleaner API. I have started writing modules that
took $c as an argument, but generally took it out later.

4. of course there are exceptions ... which is why a framework that
has flexibility and power is worth investing time to learn, even if it
takes longer than one that says "we do it this way around here ..."

this is just ideas that I've seen starting to emerge from many
learner's mistakes ... others will know different / better ...


On 12/28/06, A. Pagaltzis <pagaltzis at gmx.de> wrote:
> * Jonathan Rockway <jon at jrock.us> [2006-12-27 21:25]:
> > No, it's not.  Creating an object in Perl amounts to setting a
> > flag (the OBJECT flag in subclasses of SvPVMG, to be exact).
> >
> > See illguts: http://gisle.aas.no/perl/illguts/
>
> Are you being too literal on purpose? Yeah blessing a ref is just
> setting a flag but you need a referee for that ref and people
> don't consider its creation a separate step in general.
>
>
> * Mark Zealey <mark at itsolve.co.uk> [2006-12-27 17:20]:
> > On Wednesday 27 December 2006 1:01 pm, Ash Berlin wrote:
> > > Very very *VERY* bad idea.
> > >
> > > __PACKAGE__->mk_accessors(context);
> > >
> > > sub ACCEPT_CONTEXT {
> > >    my ($self, $c, @args) = @_;
> > >
> > >    my $new = bless({ %$self }, ref $self);
> > >    $new->context($c);
> > >    return $new;
> > > }
> >
> > Isn't that really really slow though? Constructing a new object
> > for each call?
>
> Well, it depends. If you call `$c->Model('Log')` a ton of times,
> then it will indeed be slow. If you don't, you won't notice.
>
> If you've *determined* from profiling that `$c->Model('Log')` is
> called enough to be a bottleneck, you have two options. The
> trivial one is that to store the object returned in a variable
> and then make your logging calls on that variable, so you don't
> go through `ACCEPT_CONTEXT` constantly. The other is to memoise
> `ACCEPT_CONTEXT` so when it's passed the same `$c`, it always
> returns the same copy. You'll have to be very careful about your
> cache though; it's easy to introduce leaks. If Catalyst depends
> critically on object destruction timing, you might even break the
> whole thing alltogether.
>
> In summary: avoid caring too much without specific reason to.
>
> Regards,
> --
> Aristotle Pagaltzis // <http://plasmasturm.org/>
>
> _______________________________________________
> List: Catalyst at lists.rawmode.org
> Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
> Dev site: http://dev.catalyst.perl.org/
>


-- 
Daniel McBrearty
email : danielmcbrearty at gmail.com
www.engoi.com : the multi - language vocab trainer
BTW : 0873928131



More information about the Catalyst mailing list