[Catalyst] Accessing YAML details from within the Model libraries

Jonathan Rockway jon at jrock.us
Thu Jan 18 16:45:43 GMT 2007


John Napiorkowski wrote:
> package myapp::Controller::test;
> 
> [snip snip]
> 
> sub myaction :Local {
> 
>  my ($self, $c) = @_;
> 
>  $c->log->info($self->{attr}); #onetwothree to log
> }

It would be better if you did:

   package MyApp::C::Test;
   use base 'Catalyst::Controller';
   __PACKAGE__->mk_accessors(qw/attr somethingelse whatever/);
               # ^^ important!

   sub an_action :Whatever {
      my ($self, $c, @args) = @_;
      my $attr = $self->attr;                 # get
      $self->somethingelse('something else'); # set
   }

You can set them via the config file as above:

   Controller::Test:
     attr: foo
     whatever: bar

See the Class::Accessor documentation for full details on how these
accessors work.

> 
> I haven't tested this but I image it would work from
> Views and Models as well.  Give it a try.

It works.  Views, Models, and Controllers are all Components.
Theoretically you can have components that aren't M/V/C, but I've never
done that.

-- 
package JAPH;use Catalyst qw/-Debug/;($;=JAPH)->config(name => do {
$,.=reverse qw[Jonathan tsu rehton lre rekca Rockway][$_].[split //,
";$;"]->[$_].q; ;for 1..4;$,=~s;^.;;;$,});$;->setup;



More information about the Catalyst mailing list