[Catalyst] Best way to have the controller do something just one time

John Napiorkowski jjn1056 at yahoo.com
Sun Sep 3 03:14:30 CEST 2006


Hi,

There is some initialization stuff I like to do the
first time a controller is loaded up.  I've been doing
it this way (shown to me a looooong time ago here on
this group):

sub begin :Private
{
   ($self, $c) = @_;

   unless( $self->{__init})
   {
      ## do your one time init here
      $self->{__init}++;
   }
}

This works although if feels like a hack that left one
of my Java peers snickering as well as incurs a minor
penalty for each request.

So I was looking around at some plugins and other
controllers to see what other people are doing for
this, if there was a more elegant solution for an
elegant framework.  I noticed some use of over-ridding
the COMPONENT method, but I just can't get this to
work for me.  I looked at the source for this and it
leaves me with the impression you can get some sort of
$self and $context for the controller (although this
gets called before any requests I have to wonder what
kind of context it is) but I just get all sort of
errors when I try to use it.

Here's one example that doesn't work.  Please don't
laugh too hard at me:

sub COMPONENT
{
  my $self	= shift->SUPER::new(shift @_);
  my ($c)	= @_;
	
  my $service	= 'feedrolls';

  $self->{service} =  
   $c->model('db::services::services');
	
  return $self;
}

I'm sure I'm totally buggering up this example but I
hope it illustrates what I am trying to do.  The error
in this case is that $c is an unblessed reference,
BTW.

I'd appreciate some suggestions.  Perhaps if people
throw in some other good things they did when building
base controllers it might serve as a reference for a
FAQ on this subject?  We already have a plugin
writer's guide, but as a recent discussion on this
list suggested, it might be better for a lot of stuff
to be base controllers or other components.

Thanks!

John

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the Catalyst mailing list