[Catalyst] How to execute controller at startup

John Napiorkowski jjn1056 at yahoo.com
Tue Aug 7 16:38:49 GMT 2007


--- Aditya Verma <verma.aditya1 at gmail.com> wrote:

> Hi All,
> 
> Thank you very much for all your valuable comments
> at appreciate your quick
> response.
> 
> I have one more query.
> I have implemented a Plugin. How can I call a plugin
> method from controller
> outside of the controller methods.
> 
> suppose i have a Plugin named
> "Catalyst::Plugin::MyPlugin" contains a method
> called "sub myFunc {}"
> 
> Now i want to call myFunc from controller but i dont
> have context object
> outside of methods so currently I am doing:
> Catalyst::Plugin::MyPlugin::myFunc();
> 
> which is a normal function call (not catalyst way of
> doing, not even object
> oriented).
> Could you please suggest me an efficient way to call
> that method.
> 
> Thanks,
> Aditya

Since I'm not sure what your use case is I can only
give you some vaguely general suggestions.

First of all, the community has lately pushed people
to look carefully at their plugins to see if they are
really plugins or something else.  There is a document
posted at:

http://search.cpan.org/~jrockway/Catalyst-Manual-5.700701/lib/Catalyst/Manual/ExtendingCatalyst.pod

which is a a MUST READ for all new developers.

If you have something locked up in a plugin that you
need elsewhere, like in a cron job, for example, that
probably belongs in a Plain old Perl class and brought
into Catalyst via a Model.  That way you can access
functionality outside of Catalyst.

For (very brief) example:

A Plain Old Perl module:

package Myapp::Somestuff;

use base 'Class::Accessor';

sub stuff {

  my ($self, $arg) = @_;

  return $arg x 2;
}

A Catalyst model to wrap this:

package Myapp::Web::Model::Somestuff;

use Class::C3;
use base 'Catalyst::Model';

__PACKAGE__->mk_accessors(qw/somestuff/);

sub new {
  my ($class, $c, $arguments) = @_;
  my $self = $class->next::method($c, $arguments);
  $self->somestuff(Myapp::Somestuff->new());
  return $self;
}

sub stuff {
  shift->somestuff->stuff(@_);
}

In a controller:

Package Myapp::Web::Controller::Something;

use base 'Catalyst::Controller';

sub testit :Local {

  my ($self, $c) = @_;

  $c->stash(
    result => $c->model('Somestuff')->stuff('a'),
  );
}

And then in your template you have a stash key
'result' which is equal to 'aa'.  And you have code
you can use outside of catalyst.

The above can be improved.  For example you can use
AUTOLOAD to automatically map the wrapper methods in
your module:

(Lifted pretty much from Handle or Mango, I forgot)

sub AUTOLOAD
{
  my $self = shift @_;
  my @args = @_;
  my ($method) = (our $AUTOLOAD =~ /([^:]+)$/);
  return if $method =~ /(DESTROY|ACCEPT_CONTEXT)/;

  return $self->somestuff->$method(@args) if
	 $self->somestuff->can($method);
}

Good luck!

-john

> 
> 
> On 8/6/07, John Napiorkowski <jjn1056 at yahoo.com>
> wrote:
> >
> >
> > --- Aditya Verma <verma.aditya1 at gmail.com> wrote:
> >
> > > Hi,
> > >
> > > I am new to catalyst.
> > > I have developed an application using Catalyst.
> > >
> > > I need all my controllers to execute at startup
> and
> > > register there method
> > > URLs.
> > > I am not getting how to execute controller at
> > > startup.
> > >
> > > Please help me.
> > >
> > > Thanks,
> > > Aditya
> >
> > Hi,
> >
> > Catalyst Controllers are subclasses of
> > Catalyst::Component so are automatically located a
> > registered by your primary Catalyst class at
> startup.
> > If you are finding that you don't see your
> controllers
> > and/or actions in the debug screen when you start
> the
> > development server then you must have some typos
> in
> > the controller package names or something similar.
> >
> > Please give us a little more detail about your
> trouble
> > and the actually need that you are trying to fill.
> >
> > --john
> >
> >
> >
> >
> >
> >
>
____________________________________________________________________________________
> > Be a better Heartthrob. Get better relationship
> answers from someone who
> > knows. Yahoo! Answers - Check it out.
> >
>
http://answers.yahoo.com/dir/?link=list&sid=396545433
> >
> > _______________________________________________
> > 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/
> >
> > _______________________________________________
> 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/
> 



       
____________________________________________________________________________________Ready for the edge of your seat? 
Check out tonight's top picks on Yahoo! TV. 
http://tv.yahoo.com/



More information about the Catalyst mailing list