Catalyst::Model::DataHash - Expose Perl::Data::Collection::Hash as a Per Request Model
Create a Model Subclass:
package MyApp::Model::Foo; use Moose; extends 'Catalyst::Model::DataHash'; __PACKAGE__->meta->make_immutable;
Use it in a controller:
sub myaction :Local { my ($self, $c) = @_; # Default API $c->model('DataHash')->set(a=>1, b=>2); $c->model('DataHash')->get('a'); # 1 $c->model('DataHash')->get('b'); # 2 # Alternative accessors $c->model('DataHash')->a; # 1 # RAISES AN EXCEPTION $c->model('DataHash')->not_yet_set; $c->model('DataHash')->set(a=>'already set...'); }
You might find it useful to make this your default model:
MyApp->config(default_model=>'DataHash');
So that you can use this without naming the model:
sub myaction :Local { my ($self, $c) = @_; $c->model->set(a=>1, b=>2); $c->model->get('a'); # 1 $c->model->get('b'); # 2 #alternative accessors $c->model->a; # 1 }
Which makes it less verbose. Alternatively you can use the included plugin Catalyst::Plugin::DataHash which injects a DataHash model for you into your application, and sets it to be the default model:
package MyApp; use Catalyst qw/DataHash/; MyApp->setup;
Then you can just do (in an action):
sub myaction :Local { my ($self, $c) = @_; $c->model->set(a=>1); }
The most common way that a controller shares information between actions and the view is to set key / values in the stash:
$c->stash(a=>1, b=>2);
The stash suffers from several downsides, some of which include the fact it is a global hash and is prone to typos and related confusion, like how it can auto-vivify a stash key when a value does not already exist. This Catalyst model offers an approach to providing stash-like features with a slightly less error prone interface. It is also hoped that it might inspire you to think about how to better use models in your Catalyst application to properly type your interfaces. It wraps Perl::Data, specifically Data::Perl::Collection::Hash and addes a bit of error checking and a method it ease integration with JSON encoders.
This is a 'per-request' Catalyst model, which means that each new request can get one model for use during the request duration.
This model provides the following methods, most of which are delegated to Data::Perl::Collection::Hash. Most of this documention following is copied verbosely from that distribution. When we'd adapted functionality, this is noted.
Sets the elements in the hash to the given values. It returns the new values set for each key, in the same order as the keys passed to the method.
This method requires at least two arguments, and expects an even number of arguments.
Added Behavior: If you try to set a key that already exists, this will raise an exception.
Returns a list of values in the hash for the given keys.
This method requires at least one argument.
Added Behavior: If you try to get a key that has not already been set, this will raise an exception.
Returns the list of keys in the hash.
This method does not accept any arguments.
Returns a list of values in the hash for the deleted keys.
Added Behavior: If you try to delete a key that has not already been set, this will raise an exception.
Returns true if the given key is present in the hash.
This method requires a single argument.
Returns true if the value of a given key is defined.
Returns the list of values in the hash.
Returns the key/value pairs in the hash as an array of array references.
for my $pair ( $object->option_pairs ) { print "$pair->[0] = $pair->[1]\n"; }
Returns the key/value pairs in the hash as a flattened list..
Resets the hash to an empty value, like %hash = ().
Returns the number of elements in the hash.
If passed one argument, returns the value of the specified key. If passed two arguments, sets the value of the specified key.
Added Behavior: If you try to get a key that has not already been set, this will raise an exception. If you try to set a key that has already been set, it will raise an exception.
Returns a hashref of all the existing data suitable to send to a JSON serializer.
Catalyst, Data::Perl.
John Napiorkowski email:jjnapiork@cpan.org
Copyright 2015, John Napiorkowski email:jjnapiork@cpan.org
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Catalyst::Model::DataHash, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Catalyst::Model::DataHash
CPAN shell
perl -MCPAN -e shell install Catalyst::Model::DataHash
For more information on module installation, please visit the detailed CPAN module installation guide.