File Coverage

blib/lib/Template/TAL/ValueParser.pm
Criterion Covered Total %
statement 24 25 96.0
branch 3 6 50.0
condition 2 2 100.0
subroutine 5 5 100.0
pod 2 2 100.0
total 36 40 90.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Template::TAL::ValueParser - parse attribute values
4              
5             =head1 SYNOPSIS
6              
7             my $string = "path:/foo/bar/0/baz";
8             my $value = Template::TAL::ValueParser->value( $string );
9            
10             =head1 DESCRIPTION
11              
12             This module is responsible for parsing the values of attributes in templates,
13             and returning them. It can also split multiple values up into a list, using
14             semicolons as list seperators. The way that values are parsed is based very
15             strongly on TALES (http://www.zope.org/Wikis/DevSite/Projects/ZPT/TALES)
16             but the actual TALES spec is provided by the language module
17             L.
18              
19             =cut
20              
21             package Template::TAL::ValueParser;
22 1     1   57292 use warnings;
  1         3  
  1         37  
23 1     1   5 use strict;
  1         1  
  1         39  
24 1     1   6 use Carp qw( croak );
  1         7  
  1         559  
25              
26             =head1 METHODS
27              
28             =over
29              
30             =item split( string )
31              
32             commands in 'string' can be split by ';' characters, with raw semicolons
33             represented as ';;'. This command splits the string on the semicolons, and
34             de-escapes the ';;' pairs. For instance:
35              
36             foo; bar; baz;; narf
37              
38             splits to:
39              
40             ['foo', 'bar', 'baz; narf']
41              
42             Not technically part of TALES, I think, but really useful for TAL anyway.
43              
44             =cut
45              
46             sub split {
47 4     4 1 2526 my ($class, $string) = @_;
48             # TODO this is _hokey_. Do it properly.
49 4         13 $string =~ s/;;/\x{12345}/g;
50 4         44 my @list = grep {$_} split(/\s*;\s*/, $string);
  9         24  
51 4         34 s/\x{12345}/;/g for @list;
52 4         27 return @list;
53             }
54              
55             =item value( expression, context arrayref, plugin arrayref )
56              
57             parse a TALES expression in the first param, such as
58              
59             string:Hello there
60             path:/a/b/c
61              
62             using the passed contexts (in order) to look up path values. Contexts should
63             be hashes, and we will look in each context for a defined key of the given
64             path until we find one.
65              
66             (note - I need the multiple contexts code because TAL lets you set
67             globals in define statements, so I need a local context, and a global
68             context)
69              
70             The plugins value should be an arrayref of language plugins to ask about
71             the various types of string. At a minimum, this should probably include
72             Template::TAL::Language::TALES, or the module won't do a lot.
73              
74             =cut
75              
76             sub value {
77 15     15 1 35 my ($class, $exp, $contexts, $plugins) = @_;
78 15 50       41 Carp::croak("contexts must be arrayref, not ".ref($contexts)) unless ref($contexts) eq 'ARRAY';
79 15         75 my ($type, $string) = $exp =~ /^\s*(?:(\w+):\s*)?(.*)/;
80 15   100     47 $type ||= "path";
81 15         26 my $sub = "process_tales_$type";
82 15 50       24 for my $plugin (@{ $plugins || [] }) {
  15         47  
83 15 50       59 if ($plugin->can($sub)) {
84 15         51 return $plugin->$sub($string, $contexts, $plugins );
85             }
86             }
87 0           die "unknown TALES type '$type'\n";
88             }
89              
90             =back
91              
92             =head1 COPYRIGHT
93              
94             Written by Tom Insam, Copyright 2005 Fotango Ltd. All Rights Reserved
95              
96             This program is free software; you can redistribute
97             it and/or modify it under the same terms as Perl itself.
98              
99             =cut
100              
101             1;