File Coverage

lib/Petal/Functions.pm
Criterion Covered Total %
statement 24 29 82.7
branch 6 8 75.0
condition n/a
subroutine 5 6 83.3
pod 0 4 0.0
total 35 47 74.4


line stmt bran cond sub pod time code
1             # ------------------------------------------------------------------
2             # Petal::Functions - Helper functions for the Petal.pm wrapper
3             # ------------------------------------------------------------------
4             # Author: Jean-Michel Hiver
5             # Description: This class parses a template in 'canonical syntax'
6             # (referred as the 'UGLY SYNTAX' in the manual) and generates Perl
7             # code that can be turned into a subroutine using eval().
8             # ------------------------------------------------------------------
9             package Petal::Functions;
10 77     77   484 use strict;
  77         137  
  77         2195  
11 77     77   350 use warnings;
  77         110  
  77         25309  
12              
13              
14             # find_filepath ($filename, @paths);
15             # ----------------------------------
16             # Finds the filepath for $filename in @paths
17             # and returns it.
18             sub find_filepath
19             {
20 0     0 0 0 my $filename = shift;
21 0         0 for (@_)
22             {
23 0         0 s/\/$//;
24 0 0       0 return $_ if (-e "$_/$filename");
25             }
26             }
27              
28              
29             # find_filename ($language, @paths);
30             # ----------------------------------
31             # Finds the filename for $language in @paths.
32             # For example, if $language is 'fr-CA' it might return
33             #
34             # fr-CA.html
35             # fr-CA.xml
36             # fr.html
37             # en.html
38             sub find_filename
39             {
40 16     16 0 26 my $lang = shift;
41 16         32 my @paths = @_;
42            
43 16         38 while (defined $lang)
44             {
45 22         38 foreach my $path (@paths)
46             {
47 23         45 my $filename = exists_filename ($lang, $path);
48 23 100       884 defined $filename and return $filename;
49             }
50            
51 6         20 $lang = parent_language ($lang);
52             }
53            
54 0         0 return;
55             }
56              
57              
58             # parent_language ($lang);
59             # ------------------------
60             # Returns the parent language for $lang, i.e.
61             # 'fr-CA' => 'fr' => $Petal::LANGUAGE => undef.
62             #
63             # $DEFAULT is set to 'en' by default but that can be changed, e.g.
64             # local $Petal::LANGUAGE = 'fr' for example
65             sub parent_language
66             {
67 9     9 0 1264 my $lang = shift;
68 9 100       41 $lang =~ /-/ and do {
69 5         32 ($lang) = $lang =~ /^(.*)\-/;
70 5         21 return $lang;
71             };
72            
73 4 100       14 $lang eq $Petal::LANGUAGE and return;
74 3         9 return $Petal::LANGUAGE;
75             }
76              
77              
78             # exists_filename ($language, $path);
79             # -----------------------------------
80             # looks for a file that matches $langage. in $path
81             # if the file is found, returns the filename WITH its extension.
82             #
83             # example:
84             #
85             # # $filename will be either 'en-US.html, en-US.xml, ... or 'undef'.
86             # my $filename = exists_filename ('en-US', './scratch');
87             sub exists_filename
88             {
89 26     26 0 1403 my $language = shift;
90 26         34 my $path = shift;
91            
92 26         1901 return (map { s{\Q$path\E/?}{}; $_ } <$path/$language.*>)[0];
  18         419  
  18         87  
93             }
94              
95              
96             1;
97              
98              
99             __END__