| 1 | package Data::Phrasebook; | ||||||||||||||||||
| 2 | 13 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 3 | 13 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 13 | ▶ | use base qw( Data::Phrasebook::Debug ); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 13 | ▶ | use Carp qw( croak ); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | 13 | ▶ | use vars qw($VERSION); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 13 | $VERSION = '0.35'; | |||||||||||||||||
| 9 | |||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||
| 11 | |||||||||||||||||||
| 12 | Data::Phrasebook - Abstract your queries! | ||||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | use Data::Phrasebook; | ||||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | my $q = Data::Phrasebook->new( | ||||||||||||||||||
| 19 | class => 'Plain', | ||||||||||||||||||
| 20 | loader => 'Text', | ||||||||||||||||||
| 21 | file => 'phrases.txt', | ||||||||||||||||||
| 22 | ); | ||||||||||||||||||
| 23 | |||||||||||||||||||
| 24 | # simple keyword to phrase mapping | ||||||||||||||||||
| 25 | my $phrase = $q->fetch($keyword); | ||||||||||||||||||
| 26 | |||||||||||||||||||
| 27 | # keyword to phrase mapping with parameters | ||||||||||||||||||
| 28 | $q->delimiters( qr{ \[% \s* (\w+) \s* %\] }x ); | ||||||||||||||||||
| 29 | my $phrase = $q->fetch($keyword,{this => 'that'}); | ||||||||||||||||||
| 30 | |||||||||||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | Data::Phrasebook is a collection of modules for accessing phrasebooks from | ||||||||||||||||||
| 34 | various data sources. | ||||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | =head1 PHRASEBOOKS | ||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | To explain what phrasebooks are it is worth reading Rani Pinchuk's | ||||||||||||||||||
| 39 | (author of L<Class::Phrasebook>) article on Perl.com: | ||||||||||||||||||
| 40 | |||||||||||||||||||
| 41 | L<http://www.perl.com/pub/a/2002/10/22/phrasebook.html> | ||||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | Common uses of phrasebooks are in handling error codes, accessing databases | ||||||||||||||||||
| 44 | via SQL queries and written language phrases. Examples are the mime.types | ||||||||||||||||||
| 45 | file and the hosts file, both of which use a simple phrasebook design. | ||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | Unfortunately Class::Phrasebook is a complete work and not a true class | ||||||||||||||||||
| 48 | based framework. If you can't install XML libraries, you cannot use it. | ||||||||||||||||||
| 49 | This distribution is a collaboration between Iain Truskett and myself to | ||||||||||||||||||
| 50 | create an extendable and class based framework for implementing phrasebooks. | ||||||||||||||||||
| 51 | |||||||||||||||||||
| 52 | =head1 CLASSES | ||||||||||||||||||
| 53 | |||||||||||||||||||
| 54 | In creating a phrasebook object, a class type is required. This class defines | ||||||||||||||||||
| 55 | the nature of the phrasebook or the behaviours associated with it. Currently | ||||||||||||||||||
| 56 | there are two classes, Plain and SQL. | ||||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | The Plain class is the default class, and allows retrieval of phrases via the | ||||||||||||||||||
| 59 | fetch() method. The fetch() simply returns the phrase that maps to the given | ||||||||||||||||||
| 60 | keyword. | ||||||||||||||||||
| 61 | |||||||||||||||||||
| 62 | The SQL class allows specific database handling. Phrases are retrieved via the | ||||||||||||||||||
| 63 | query() method. The query() method internally retrieves the SQL phrase, then | ||||||||||||||||||
| 64 | returns the statement handler object, which the user can then perform a | ||||||||||||||||||
| 65 | prepare/execute/fetch/finish sequence on. For more details see | ||||||||||||||||||
| 66 | Data::Phrasebook::SQL. | ||||||||||||||||||
| 67 | |||||||||||||||||||
| 68 | =head1 CONSTRUCTOR | ||||||||||||||||||
| 69 | |||||||||||||||||||
| 70 | =head2 new | ||||||||||||||||||
| 71 | |||||||||||||||||||
| 72 | The arguments to new depend upon the exact class you're creating. | ||||||||||||||||||
| 73 | |||||||||||||||||||
| 74 | The default class is C<Plain> and only requires the Loader arguments. The | ||||||||||||||||||
| 75 | C<SQL> class requires a database handle as well as the Loader arguments. | ||||||||||||||||||
| 76 | |||||||||||||||||||
| 77 | The C<class> argument defines the object class of the phrasebook and the | ||||||||||||||||||
| 78 | behaviours that can be associated with it. Using C<Foobar> as a fake class, | ||||||||||||||||||
| 79 | the class module is searched for in the following order: | ||||||||||||||||||
| 80 | |||||||||||||||||||
| 81 | =over 4 | ||||||||||||||||||
| 82 | |||||||||||||||||||
| 83 | =item 1 | ||||||||||||||||||
| 84 | |||||||||||||||||||
| 85 | If you've subclassed C<Data::Phrasebook>, for example as C<Dictionary>, | ||||||||||||||||||
| 86 | then C<Dictionary::Foobar> is tried. | ||||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | =item 2 | ||||||||||||||||||
| 89 | |||||||||||||||||||
| 90 | If that failed, C<Data::Phrasebook::Foobar> is tried. | ||||||||||||||||||
| 91 | |||||||||||||||||||
| 92 | =item 3 | ||||||||||||||||||
| 93 | |||||||||||||||||||
| 94 | If B<that> failed, C<Foobar> is tried. | ||||||||||||||||||
| 95 | |||||||||||||||||||
| 96 | =item 4 | ||||||||||||||||||
| 97 | |||||||||||||||||||
| 98 | If all the above failed, we croak. | ||||||||||||||||||
| 99 | |||||||||||||||||||
| 100 | =back | ||||||||||||||||||
| 101 | |||||||||||||||||||
| 102 | This should allow you some flexibility in what sort of classes | ||||||||||||||||||
| 103 | you use while not having you type too much. | ||||||||||||||||||
| 104 | |||||||||||||||||||
| 105 | For other parameters, see the specific class you wish to instantiate. | ||||||||||||||||||
| 106 | The class argument is removed from the arguments list and the C<new> | ||||||||||||||||||
| 107 | method of the specified class is called with the remaining arguments. | ||||||||||||||||||
| 108 | |||||||||||||||||||
| 109 | =cut | ||||||||||||||||||
| 110 | |||||||||||||||||||
| 111 | 22 | ▶ | sub new { | ||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||
| 112 | 22 | my $class = shift; | |||||||||||||||||
| 113 | 22 | my %args = @_; | |||||||||||||||||
| 114 | |||||||||||||||||||
| 115 | 22 | ▶ | my $debug = delete $args{debug} || 0; | ||||||||||||||||
|
Condition100%2/2
delete $args{'debug'} || 0
MC/DC100%1/1
delete $args{'debug'} || 0
delete $args{'debug'}
Truth table100%2/2
A
A= delete $args{'debug'}
| |||||||||||||||||||
| 116 | 22 | $class->debug($debug); | |||||||||||||||||
| 117 | |||||||||||||||||||
| 118 | 22 | ▶ | if($debug) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 119 | 1 | $class->store(3,"$class->new IN"); | |||||||||||||||||
| 120 | 1 | $class->store(4,"$class->new args=[".$class->dumper(\%args).']'); | |||||||||||||||||
| 121 | } | ||||||||||||||||||
| 122 | |||||||||||||||||||
| 123 | 22 | ▶ | my $sub = delete $args{class} || 'Plain'; | ||||||||||||||||
|
Condition100%2/2
delete $args{'class'} || 'Plain'
MC/DC100%1/1
delete $args{'class'} || 'Plain'
delete $args{'class'}
Truth table100%2/2
A
A= delete $args{'class'}
| |||||||||||||||||||
| 124 | 22 | ▶ | if (eval "require ${class}::$sub") { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 125 | 17 | $sub = $class."::$sub"; | |||||||||||||||||
| 126 | } elsif (eval "require Data::Phrasebook::$sub") { | ||||||||||||||||||
| 127 | 3 | $sub = "Data::Phrasebook::$sub"; | |||||||||||||||||
| 128 | } elsif (eval "require $sub") { | ||||||||||||||||||
| 129 | # it's a module by itself | ||||||||||||||||||
| 130 | } else { | ||||||||||||||||||
| 131 | 1 | croak "Could not find appropriate class for '$sub': [$@]"; | |||||||||||||||||
| 132 | } | ||||||||||||||||||
| 133 | |||||||||||||||||||
| 134 | 21 | ▶ | $class->store(4,"$class->new sub=[$sub]") if($class->debug); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 135 | |||||||||||||||||||
| 136 | 21 | return $sub->new( %args ); | |||||||||||||||||
| 137 | } | ||||||||||||||||||
| 138 | |||||||||||||||||||
| 139 | 13 | 1; | |||||||||||||||||
| 140 | |||||||||||||||||||
| 141 | __END__ | ||||||||||||||||||