| CGI-Application-Plugin-Phrasebook documentation | Contained in the CGI-Application-Plugin-Phrasebook distribution. |
CGI::Application::Plugin::Phrasebook - A CGI::Application plugin for Data::Phrasebook
package MyCGIApp;
use base 'CGI::Application';
use CGI::Application::Plugin::Phrasebook;
sub cgiapp_prerun {
my $self = shift;
$self->config_phrasebook(
class => 'Plain',
loader => 'YAML',
file => 'conf/my_phrasebook.yml',
);
# ... do other stuff here ...
}
sub some_run_mode {
my $self = shift;
# grab the phrasebook instance
# and fetch a keyword from it
return $self->phrasebook->fetch('a_phrasebook_keyword');
}
This is a very simple plugin which provides access to an instance
(or instances) of Data::Phrasebook inside your CGI::Application.
I could have just stuffed this in with param, but this way is much
nicer (and easier to type).
Given an array of arguments, this configures your Data::Phrasebook
instance by simply passing any arguments onto Data::Phrasebook::new.
It then stashes it into the CGI::Application instance.
If given a HASH reference, this will configure multiple Data::Phrasebook instances, one for each hash key. Here is an example, of how that would be used.
package MyCGIApp;
use base 'CGI::Application';
use CGI::Application::Plugin::Phrasebook;
sub cgiapp_prerun {
my $self = shift;
$self->config_phrasebook({
my_yaml_phrasebook => {
class => 'Plain',
loader => 'YAML',
file => 'conf/my_phrasebook.yml',
},
my_txt_phrasebook => {
class => 'Plain',
loader => 'Text',
file => 'conf/my_phrasebook.txt',
}
});
# ... do other stuff here ...
}
sub some_run_mode {
my $self = shift;
return $self->phrasebook('my_yaml_phrasebook')->fetch('a_phrasebook_keyword');
}
sub some_other_run_mode {
my $self = shift;
return $self->phrasebook('my_txt_phrasebook')->fetch('a_phrasebook_keyword');
}
You can also assign one of the hash keys to the string __DEFAULT__,
and it will be used as the default phrasebook. But this behavior is
optional.
This will return the Data::Phrasebook instance, or undef if one
has not yet been configured.
If the plugin has been configured with multiple phrasebooks, you can
specify the particular $phrasebook_name, if one is not specified,
it will attempt to use the phrasebook called __DEFAULT__. If the
phrasebook is not found, undef is returned.
All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT.
Stevan Little <stevan@iinteractive.com>
Copyright 2006 by Infinity Interactive, Inc.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| CGI-Application-Plugin-Phrasebook documentation | Contained in the CGI-Application-Plugin-Phrasebook distribution. |
package CGI::Application::Plugin::Phrasebook; use strict; use warnings; use Carp 'confess'; use Scalar::Util 'blessed'; use CGI::Application; use Data::Phrasebook; our $VERSION = '0.02'; our @EXPORT = qw( config_phrasebook phrasebook ); sub import { my $pkg = shift; my $call = caller; no strict 'refs'; foreach my $sym (@EXPORT) { *{"${call}::$sym"} = \&{$sym}; } } sub config_phrasebook { my $self = shift; unless (ref($_[0])) { $self->{'__PHRASEBOOK'}{'pb'}{'__DEFAULT__'} = Data::Phrasebook->new(@_); return; } foreach my $name (keys %{$_[0]}) { $self->{'__PHRASEBOOK'}{'pb'}{$name} = Data::Phrasebook->new(%{$_[0]->{$name}}); } } sub phrasebook { my $self = shift; return $self->{'__PHRASEBOOK'}{'pb'}{'__DEFAULT__'} unless @_; return $self->{'__PHRASEBOOK'}{'pb'}{$_[0]}; } 1; __END__