[Catalyst] Creating my own controller and another question

LU, ZHENYUAN [AG/1000] zhenyuan.lu at monsanto.com
Fri Apr 28 18:21:23 CEST 2006


The problem is due to the way config is implemented in Catalyst. It is
stored as a reference to a hash in _config which is a
Class::Data::Inheritable. 

101 	sub config {
102 	    my $self = shift;
103 	    $self->_config( {} ) unless $self->_config;
104 	    if (@_) {
105 	        my $config = @_ > 1 ? {@_} : $_[0];
106 	        while ( my ( $key, $val ) = each %$config ) {
107 	            $self->_config->{$key} = $val;
108 	        }
109 	    }
110 	    return $self->_config;
111 	}

After it is initiated in base class, when any class inheriting from it
changes config, no new config hash is created in 103 and only the values in
the hash referenced by _config are changed. So all extended classes actually
share the same config hash. When different classes changes the value of the
same key, the last changes wins.  

-----Original Message-----
From: catalyst-bounces at lists.rawmode.org
[mailto:catalyst-bounces at lists.rawmode.org] On Behalf Of John Napiorkowski
Sent: Friday, April 28, 2006 10:11 AM
To: The elegant MVC web framework
Subject: Re: [Catalyst] Creating my own controller and another question


Thanks for such a quick reply.  I'm still shaking my
head about the strange config problem.  I think I'll
have to break out a separate instance of catalyst to
clarify it and try to write a test case for it.

One thing that I am rather confused about is how and
when $self and $c appear or not.  I got that you have
to use the "my ($self, $c) = @_);" and not try to
shift them in the way I am used to with other perl
development.  However I get into a lot of trouble when
I need the $c but only have $self (such as when I have
some controller accessors) or I have $c but not $self
(such as is the default with template toolkit
templates).  I am not sure what you mean when you say:

> when Catalyst retrieves a component of any type for
> use, if the component has
> an ACCEPT_CONTEXT method it calls that with $c as an
> argument and returns the 
> result.

For example, in the code snippet you suggested I could
used to override the new subroutine for a subclass of controller, would
there be any way to get the $c context object?

> sub new {
>    my $self = shift;
>    my $new = $self->NEXT::new(@_);
>    $new->init;
>    return $new;
> }



More information about the Catalyst mailing list