[Catalyst] Re: Accessing $c from Model

A. Pagaltzis pagaltzis at gmx.de
Thu Dec 28 02:31:10 GMT 2006


* 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/>



More information about the Catalyst mailing list