File Coverage

blib/lib/Data/Phrasebook.pm
Criterion Covered Total %
statement 29 29 100.0
branch 10 10 100.0
condition 4 4 100.0
subroutine 6 6 100.0
pod 1 1 100.0
total 50 50 100.0


line stmt bran cond sub pod time code
1             package Data::Phrasebook;
2 13     13   424593 use strict;
  13         29  
  13         613  
3 13     13   67 use warnings FATAL => 'all';
  13         22  
  13         702  
4 13     13   71 use base qw( Data::Phrasebook::Debug );
  13         30  
  13         8786  
5 13     13   76 use Carp qw( croak );
  13         22  
  13         694  
6              
7 13     13   68 use vars qw($VERSION);
  13         21  
  13         4530  
8             $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) article on Perl.com:
40              
41             L
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 and only requires the Loader arguments. The
75             C class requires a database handle as well as the Loader arguments.
76              
77             The C argument defines the object class of the phrasebook and the
78             behaviours that can be associated with it. Using C 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, for example as C,
86             then C is tried.
87              
88             =item 2
89              
90             If that failed, C is tried.
91              
92             =item 3
93              
94             If B failed, C 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
107             method of the specified class is called with the remaining arguments.
108              
109             =cut
110              
111             sub new {
112 22     22 1 7188 my $class = shift;
113 22         154 my %args = @_;
114              
115 22   100     143 my $debug = delete $args{debug} || 0;
116 22         148 $class->debug($debug);
117              
118 22 100       93 if($debug) {
119 1         10 $class->store(3,"$class->new IN");
120 1         10 $class->store(4,"$class->new args=[".$class->dumper(\%args).']');
121             }
122              
123 22   100     120 my $sub = delete $args{class} || 'Plain';
124 22 100       1439 if (eval "require ${class}::$sub") {
    100          
    100          
125 17         55 $sub = $class."::$sub";
126             } elsif (eval "require Data::Phrasebook::$sub") {
127 3         10 $sub = "Data::Phrasebook::$sub";
128             } elsif (eval "require $sub") {
129             # it's a module by itself
130             } else {
131 1         239 croak "Could not find appropriate class for '$sub': [$@]";
132             }
133              
134 21 100       356 $class->store(4,"$class->new sub=[$sub]") if($class->debug);
135              
136 21         172 return $sub->new( %args );
137             }
138              
139             1;
140              
141             __END__