[Catalyst] Accessing a Controller from ~/script

Dermot paikkos at googlemail.com
Mon Mar 9 19:51:45 GMT 2009


2009/3/9 Andrew Rodland <arodland at comcast.net>:
> On Monday 09 March 2009 12:54:34 pm Dermot wrote:
>> 2009/2/19 Kieren Diment <kieren at diment.org>:
>> > On 19/02/2009, at 11:58 PM, Dermot wrote:
>> >> Wow! that works but I am not sure where ACCEPT_CONTEXT comes into it.
>> >
>> > That'd be if you needed to pass stuff from $c into the model to get it
>> > working properly.
>>
>> I'd like to allow my stand alone model access to the configuration
>> values contained in MyApp->config. It seems like the tidy way to do
>> things. Does the ACCEPT_CONTEXT method allow me to do this? Is there
>> some kinda SUPER way I can grab config and make an accessor of it?
>
> Your Model has its own bit of config space, and this config info is passed to
> its constructor. The advisable thing to do here is to have your config there,
> and have your Model class pass that info along to your backend model class.
> If you need some more complex interaction then you can have more glue in your
> Model class (and ACCEPT_CONTEXT as necessary), but the goal here is not to
> influence your backend class. If the backend class depends on any class
> with 'Catalyst' in the name, or it depends on your app being around so it can
> rummage around in the config structure, then you can't really instantiate it
> outside of Catalyst, so what's the point of splitting it out? "Loose
> coupling" is the word.

The parts of the config I want to access are things like literal paths
and other values, they are not specific to Catalyst but they are
critical to my application. I have, perhaps wrongly, put all the
values needed by my application into the configuration file. If I want
users to set run-time values, say via a form, the limitations of my
approach will soon show up. But I'll put that on the TODO list for
now.


Currently I load the configuration into the stand alone Class with a
combination of FindBin and Config::General. I thought I could avoid
that seeming as config is already in my context ($c). So I did:

package MyApp::Model::MyAdaptor;
use strict;
use warnings;
use base 'Catalyst::Model::Adaptor';
__PACKAGE__->config( class => 'MyImporter');
__PACKAGE__->mk_accessors(qw/config/);
sub ACCEPT_CONTEXT {
    my ($self, $c ) = @_;
    $self = bless({ %$self,
                     config => $c->config(''),
                 }, ref($self));
     return $self;
}
1;

but this is giving me an error:

Couldn't instantiate component "MyApp::Model::MyAdaptor", "Can't use
string ("MyApp::Model::MyAdaptor") as a HASH ref while "strict refs"
in use at /usr/lib/perl5/site_perl/5.8.6/Class/Accessor/Fast.pm line
38."

The Moose example also gives me a error:

Couldn't instantiate component "MyApp::Model::MyAdaptor", "need class
at ...~/5.8.6/Catalyst.pm line 2074"

Have I set up the Adaptor incorrectly? The Adaptor class works fine
without the ACCEPT_CONTEXT method so I suspect have done something
wrong.

Thanx,
Dp.



More information about the Catalyst mailing list