On Tue, Nov 18, 2008 at 6:13 AM, J. Shirley <[EMAIL PROTECTED]> wrote: > On Mon, Nov 17, 2008 at 11:49 PM, <[EMAIL PROTECTED]> wrote: >> HI all: >> I am a Catalyst beginner and run into this problem >> since I have a model like this >> >> package MyApp::Model::Trial; >> use strict; >> use warnings; >> >> sub new{ >> my $pack = shift; >> my $self = bless { >> foo => 'default value foo', >> },$pack; >> return $self; >> } >> >> sub get{ >> return shift->{'foo'}; >> } >> 1 >> >> how could I pass this "foo" to the controller? >> I tried some code like this >> my $a = $c->model('Trial::new'); >> $c->stash->{word} = $a->get; >> >> or >> my $a = $c->model('Trial'); >> $c->stash->{word} = $a->get; >> >> both of them run into some exceptions >> >> >> thx for helping > > > First off, models are instantiated by Catalyst so if you create your > own 'new' method you are not going to be doing the right thing. > > Second, if all you are trying to do is having default configured > values it is much better to use accessors and let Catalyst handle the > configuration, like so: > > package MyApp::Model::Trial; > > use warnings; > use strict; > > use base 'Catalyst::Model'; # You must inherit from this > use Class::Accessor::Fast; # Use this to generate the accessor methods > > # Make an accessor for 'foo' > __PACKAGE__->mk_accessors('foo'); > > __PACKAGE__->config( > 'foo' => 'default foo value' > ); > > 1; > > __END__ > > That really is all there is to it. When Catalyst loads, it will load > everything in the Controller, Model and View namespaces based on the > base classes (Catalyst::Model, which in turn inherits from a common > base class called Catalyst::Component) > > Now to acess 'foo' you can just use the simple accessor (and setter): > > $c->model('Trial')->foo; > # or > $c->model('Trial')->foo("new value"); >
Actually, I made a slight mistake because I was going down the Moose route first and then changed it. You also need to inherit from Class::Accessor::Fast... So, use: use base qw/Catalyst::Model Class::Accessor::Fast/; -J _______________________________________________ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/