File Coverage

blib/lib/Parse/YALALR/Common.pm
Criterion Covered Total %
statement 38 62 61.2
branch 0 2 0.0
condition n/a
subroutine 11 18 61.1
pod n/a
total 49 82 59.7


line stmt bran cond sub pod time code
1             package Parse::YALALR::Common;
2              
3             # API:
4             # use Parse::YALALR::Common;
5             # print "Normal: $x Escaped: $E{$x}\n"; # (XML escaping)
6             # print "You are holding $n $P{'ball',$n}\n"; # (pluralization)
7              
8 1     1   5 use strict;
  1         2  
  1         38  
9 1     1   5 no strict 'refs';
  1         2  
  1         1221  
10              
11             sub import {
12 3     3   9 &Parse::YALALR::Common::XMLEscape::import;
13 3         7 &Parse::YALALR::Common::XMLUnescape::import;
14 3         6 &Parse::YALALR::Common::Identity::import;
15 3         7 &Parse::YALALR::Common::Plural::import;
16             }
17              
18              
19             ###################### XMLEscape, XMLUnescape ########################
20              
21             package Parse::YALALR::Common::XMLEscape;
22             sub escape ($) {
23 0     0   0 my $x = shift;
24 0         0 $x =~ s/\&/\&/g; # Must come first!
25 0         0 $x =~ s/<-/\&larrow;/g;
26 0         0 $x =~ s/->/\&arrow;/g;
27 0         0 $x =~ s/<=/\&dlarrow;/g;
28 0         0 $x =~ s/=>/\&darrow;/g;
29 0         0 $x =~ s/
30 0         0 $x =~ s/>/\>/g;
31 0         0 $x;
32             }
33              
34 0     0   0 sub FETCH { escape($_[1]) }
35 3     3   22 sub TIEHASH { bless {}, __PACKAGE__ }
36              
37             sub import {
38 3     3   8 my $p = caller(1); # caller(0) eq Parse::Vipar::Common
39 3         4 tie %{"${p}::E"}, __PACKAGE__;
  3         25  
40 3         5 *{"${p}::E"} = \%{"${p}::E"}; # use vars '%E' for caller
  3         11  
  3         10  
41             }
42              
43             package Parse::YALALR::Common::XMLUnescape;
44              
45             sub unescape ($) {
46 0     0   0 my $x = shift;
47 0         0 $x =~ s/\</
48 0         0 $x =~ s/\>/>/g;
49 0         0 $x =~ s/\&arrow;/->/g;
50 0         0 $x =~ s/\&larrow;/<-/g;
51 0         0 $x =~ s/\&darrow;/=>/g;
52 0         0 $x =~ s/\&dlarrow;/<=/g;
53 0         0 $x =~ s/\&ldarrow;/<=/g;
54 0         0 $x;
55             }
56              
57 0     0   0 sub FETCH { unescape($_[1]) }
58 3     3   19 sub TIEHASH { bless {}, __PACKAGE__ }
59              
60             sub import {
61 3     3   5 my $p = caller(1);
62 3         4 tie %{"${p}::U"}, __PACKAGE__;
  3         20  
63 3         3 *{"${p}::U"} = \%{"${p}::U"}; # use vars '%U' for caller
  3         9  
  3         9  
64             }
65              
66             ###################### Plural ########################
67              
68             package Parse::YALALR::Common::Plural;
69             sub pluralize ($) {
70 0     0   0 my ($word, $n) = split(/$;/, shift(), 2);
71 0 0       0 return ($n == 1) ? $word : "${word}s";
72             }
73 0     0   0 sub FETCH { pluralize($_[1]) }
74 3     3   17 sub TIEHASH { bless {}, __PACKAGE__ }
75              
76             sub import {
77 3     3   13 my $p = caller(1);
78 3         4 tie %{"${p}::P"}, __PACKAGE__;
  3         22  
79 3         4 *{"${p}::P"} = \%{"${p}::P"}; # use vars '%E' for caller
  3         60  
  3         9  
80             }
81              
82             ###################### Identity ########################
83              
84             package Parse::YALALR::Common::Identity;
85 0     0   0 sub FETCH { $_[1] }
86 3     3   17 sub TIEHASH { bless {}, __PACKAGE__ }
87              
88             sub import {
89 3     3   6 my $p = caller(1);
90 3         5 tie %{"${p}::ID"}, __PACKAGE__;
  3         18  
91 3         4 *{"${p}::ID"} = \%{"${p}::ID"}; # use vars '%ID' for caller
  3         8  
  3         9  
92             }
93              
94             1;