| 1 | package Data::Phrasebook::Plain; | ||||||||||||||
| 2 | 10 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 10 | ▶ | use warnings FATAL => 'all'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 10 | ▶ | use base qw( Data::Phrasebook::Generic Data::Phrasebook::Debug ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 10 | ▶ | use Carp qw( croak ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 10 | ▶ | use vars qw($VERSION); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 10 | $VERSION = '0.35'; | |||||||||||||
| 9 | |||||||||||||||
| 10 | =head1 NAME | ||||||||||||||
| 11 | |||||||||||||||
| 12 | Data::Phrasebook::Plain - The Simple Phrasebook Model. | ||||||||||||||
| 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 | my $r = Data::Phrasebook->new( file => 'phrases.txt' ); | ||||||||||||||
| 25 | |||||||||||||||
| 26 | # simple keyword to phrase mapping | ||||||||||||||
| 27 | my $phrase = $q->fetch($keyword); | ||||||||||||||
| 28 | |||||||||||||||
| 29 | # keyword to phrase mapping with parameters | ||||||||||||||
| 30 | $q->delimiters( qr{ \[% \s* (\w+) \s* %\] }x ); | ||||||||||||||
| 31 | my $phrase = $q->fetch($keyword,{this => 'that'}); | ||||||||||||||
| 32 | |||||||||||||||
| 33 | =head1 DESCRIPTION | ||||||||||||||
| 34 | |||||||||||||||
| 35 | This module is the fallback or default phrasebook class. It doesn't do much | ||||||||||||||
| 36 | except act as a very simple templating facility. | ||||||||||||||
| 37 | |||||||||||||||
| 38 | =head1 METHODS | ||||||||||||||
| 39 | |||||||||||||||
| 40 | =head2 fetch | ||||||||||||||
| 41 | |||||||||||||||
| 42 | Retrieves the specified C<template> and substitutes any C<keywords> for | ||||||||||||||
| 43 | C<values>. | ||||||||||||||
| 44 | |||||||||||||||
| 45 | Thus, given: | ||||||||||||||
| 46 | |||||||||||||||
| 47 | hello=Hello [% where %]! | ||||||||||||||
| 48 | |||||||||||||||
| 49 | And code: | ||||||||||||||
| 50 | |||||||||||||||
| 51 | my $text = $q->fetch( 'hello', { | ||||||||||||||
| 52 | where => 'world' | ||||||||||||||
| 53 | } ); | ||||||||||||||
| 54 | |||||||||||||||
| 55 | Return value is: | ||||||||||||||
| 56 | |||||||||||||||
| 57 | Hello world! | ||||||||||||||
| 58 | |||||||||||||||
| 59 | The delimiters are deliberately taken from L<Template> Toolkit. | ||||||||||||||
| 60 | |||||||||||||||
| 61 | =cut | ||||||||||||||
| 62 | |||||||||||||||
| 63 | 18 | ▶ | sub fetch { | ||||||||||||
|
Subroutine
fetch: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||
| 64 | 18 | my $self = shift; | |||||||||||||
| 65 | 18 | my ($id, $args) = @_; | |||||||||||||
| 66 | |||||||||||||||
| 67 | 18 | ▶ | $self->store(3,"->fetch IN - @_") if($self->debug); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | |||||||||||||||
| 69 | 18 | my $map = $self->data($id); | |||||||||||||
| 70 | 16 | ▶ | croak "No mapping for '$id'" unless($map); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | 15 | my $delim_RE = $self->delimiters; | |||||||||||||
| 72 | 15 | ▶ | croak "Mapping for '$id' not a string." if ref $map; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | |||||||||||||||
| 74 | 15 | ▶ | if($self->debug) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | 2 | $self->store(4,"->fetch delimiters=[$delim_RE]"); | |||||||||||||
| 76 | 2 | $self->store(4,"->fetch args=[".$self->dumper($args)."]"); | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | |||||||||||||||
| 79 | 15 | $map =~ s{$delim_RE}[ | |||||||||||||
| 80 | defined $args->{$1} ? $args->{$1} : | ||||||||||||||
| 81 | 9 | ▶ | $self->{blank_args} ? '' : croak "No value given for '$1'"; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 82 | ]egx; | ||||||||||||||
| 83 | |||||||||||||||
| 84 | 14 | return $map; | |||||||||||||
| 85 | } | ||||||||||||||
| 86 | |||||||||||||||
| 87 | 10 | 1; | |||||||||||||
| 88 | |||||||||||||||
| 89 | __END__ | ||||||||||||||