[Catalyst] Two questions regarding components

Christian Hansen ch at ngmedia.com
Tue Jun 28 17:24:53 CEST 2005

On 2005-06-28, at 16.55, Bernhard Bauer wrote:

> 1. As far as I can tell, every component is require'd and instantiated 
> via new(). The instances are then stored in 
> $c->components->{$component_name}, making them some sort of singleton 
> objects. While this makes sense to me for controllers (which don't 
> carry a state) and views (which really are Adapters to the respective 
> template system), I see some problems here for models.

All packages that lives in MyApp::[MVC]|Model|View|Controller are 
loaded during setup, but only components that inherit from 
Catalyst::Base are instantiated.

> Specifically, is there a way to make new() in the base class for your 
> application models setup the model (and return the package name for 
> example) while having new() in a derived class return a new instance? 
> Or could the Engine maybe call a method shared_instance() which 
> returns the singleton instance of the component, while new() returns a 
> new instance every time it is called?

You don't have to do anything, just don't inherit from Catalyst::Base 
and your component will be loaded and available in $c->component. 
$c->component('MyApp::Model::Package') will return the class.

> 2. Is there a way for a plugin (or component) which depends on another 
> plugin to load it? For example, have a controller which uses 
> Catalyst::Plugin::Textile load it at startup without having to 
> explicitly specify it in the plugin list?

Currently not, but i usually document it and do a check in setup:

sub setup {
     my $c = shift;

     unless ( $c->isa('Catalyst::Plugin::Textile') ) {
         die __PACKAGE__ . ' requires Catalyst::Plugin::Textile';



Christian Hansen
+46 40 660 17 50

More information about the Catalyst mailing list