File Coverage

blib/lib/Dotiac/DTL/Addon/case_insensitive.pm
Criterion Covered Total %
statement 89 122 72.9
branch 31 76 40.7
condition 11 30 36.6
subroutine 10 10 100.0
pod 1 2 50.0
total 142 240 59.1


line stmt bran cond sub pod time code
1             ###############################################################################
2             #case-insensitive.pm
3             #Last Change: 2009-02-09
4             #Copyright (c) 2009 Marc-Seabstian "Maluku" Lucksch
5             #Version 0.4
6             ####################
7             #This file is an addon to the Dotiac::DTL project.
8             #http://search.cpan.org/perldoc?Dotiac::DTL
9             #
10             #case-insensitive.pm is published under the terms of the MIT license, which
11             #basically means "Do with it whatever you want". For more information, see the
12             #license.txt file that should be enclosed with this distribution. A copy of
13             #the license is (at the time of writing) also available at
14             #http://www.opensource.org/licenses/mit-license.php .
15             ###############################################################################
16            
17            
18             package Dotiac::DTL::Addon::case_insensitive;
19 1     1   183596 use strict;
  1         3  
  1         83  
20 1     1   7 use warnings;
  1         1  
  1         84  
21            
22             #If it is not already loaded.
23             require Dotiac::DTL::Core;
24            
25             our $VERSION=0.4;
26            
27             my $old;
28            
29             our %keymap=();
30            
31             sub import {
32 1     1   5 no warnings qw/redefine/;
  1         2  
  1         110  
33 22     22   46567 $old = \&Dotiac::DTL::devar_var;
34 22         99 *Dotiac::DTL::devar_var=\&devar_var;
35            
36             }
37             sub unimport {
38 1     1   6 no warnings qw/redefine/;
  1         2  
  1         1282  
39 22     22   71620 *Dotiac::DTL::devar_var = $old;
40             }
41            
42             sub devar_var {
43 56     56 1 4359 my $name=shift;
44 56         76 my $n=$name;
45 56 50       726 return Dotiac::DTL::Value->safe(undef) unless defined $name;
46 56         89 my $lcn = lc($name);
47 56         68 my $param=shift;
48 56         177 my $f=substr $name,0,1;
49 56         96 my $l=substr $name,-1,1;
50 56         80 my $escape=shift;
51            
52 56 50 33     299 return Dotiac::DTL::Value->safe(substr $name,1,-1) if $f eq "'" and $l eq "'" or $f eq '"' and $l eq '"';
      33        
      33        
53 56 100 66     171 return Dotiac::DTL::Value->safe(Dotiac::DTL::descap(substr $name,1,-1)) if $f eq "`" and $l eq "`";
54            
55 48 50 33     186 if ($lcn eq "block.super" and $param->{"block.super"}) {
56 0 0       0 return Dotiac::DTL::Value->safe($param->{"block.super"}->string($param,@_)) if Scalar::Util::blessed($param->{"block.super"});
57 0 0       0 return Dotiac::DTL::Value->safe($param->{"block.super"}->($param,@_)) if ref $param->{"block.super"} eq "CODE";
58             }
59 48 100       227 return Dotiac::DTL::Value->new($param->{$name},!$escape) if exists $param->{$name};
60 44 100 66     225 return Dotiac::DTL::Value->new($param->{$Dotiac::DTL::Addon::case_insensitive::keymap{$lcn}},!$escape) if defined $Dotiac::DTL::Addon::case_insensitive::keymap{$lcn} and exists($param->{$Dotiac::DTL::Addon::case_insensitive::keymap{$lcn}});
61 33         37 foreach my $k (keys %{$param}) {
  33         97  
62 33 100       119 if (lc($k) eq $lcn) {
63 1         4 $Dotiac::DTL::Addon::case_insensitive::keymap{$lcn}=$k;
64 1         13 return Dotiac::DTL::Value->new($param->{$k},!$escape);
65             }
66             }
67 32         199 my @tree=split/\./,$name;
68 32         58 $name=shift @tree;
69 32         44 $lcn = lc($name);
70 32 100 33     176 if (exists $param->{$name}) {
    50          
71 8         15 $param=$param->{$name};
72             }
73             elsif (defined $Dotiac::DTL::Addon::case_insensitive::keymap{$lcn} and exists $param->{$Dotiac::DTL::Addon::case_insensitive::keymap{$lcn}}) {
74 24         51 $param=$param->{$Dotiac::DTL::Addon::case_insensitive::keymap{$lcn}};
75             }
76             else {
77 0         0 my $found=0;
78 0         0 foreach my $k (keys %$param) {
79 0 0       0 if (lc($k) eq $lcn) {
80 0         0 $Dotiac::DTL::Addon::case_insensitive::keymap{$lcn}=$k;
81 0         0 $found=1;
82 0         0 $param=$param->{$k};
83 0         0 last;
84             }
85             }
86 0 0       0 unless ($found) {
87 0 0       0 return Dotiac::DTL::Value->safe($n) if $n!~/[^\d\-\.\,\e]/;
88 0         0 foreach my $k (keys %Dotiac::DTL::cycle) {
89 0 0 0     0 if (lc($k) eq $lcn and $Dotiac::DTL::cycle{$k}->[1]) {
90 0 0       0 return Dotiac::DTL::Value->safe("") if $Dotiac::DTL::included{"cycle_$k"}++;
91 0         0 my $r=devar_raw($Dotiac::DTL::cycle{$k}->[2]->[$Dotiac::DTL::cycle{$k}->[0]-1 % $Dotiac::DTL::cycle{$k}->[1]],$param,$escape,@_);
92 0         0 $Dotiac::DTL::included{"cycle_$k"}=0;
93 0         0 return $r;
94             }
95             }
96 0         0 return Dotiac::DTL::Value->safe(undef) ;
97             }
98             }
99 32         222 while (defined(my $name = shift @tree)) {
100 32         47 $lcn = lc($name);
101 32         72 my $r = Scalar::Util::reftype($param);
102 32 50       59 if ($r) {
103 32 50       126 if ($r eq "HASH") {
    0          
104 32 100       127 if (not exists $param->{$name}) {
105 28         37 my $found=0;
106 28         27 foreach my $k (keys %{$param}) {
  28         72  
107 12 50       52 if (lc($k) eq $lcn) {
108 12         17 $found=1;
109 12         17 $param=$param->{$k};
110 12         16 last;
111             }
112             }
113 28 100       87 next if $found;
114 16 50       69 return Dotiac::DTL::Value->safe(undef) unless Scalar::Util::blessed($param);
115             }
116             else {
117 4         5 $param=$param->{$name};
118 4         13 next;
119             }
120             }
121             elsif ($r eq "ARRAY") {
122 0 0       0 if ($name=~m/\D/) {
123 0 0       0 return Dotiac::DTL::Value->safe(undef) unless Scalar::Util::blessed($param);
124             }
125             else {
126 0 0       0 if (not exists $param->[$name]) {
127 0 0       0 return Dotiac::DTL::Value->safe(undef) unless Scalar::Util::blessed($param);
128             }
129             else {
130 0         0 $param=$param->[$name];
131 0         0 next;
132             }
133             }
134             }
135             }
136 16 50       53 if (Scalar::Util::blessed($param)) {
137 16 50       32 return Dotiac::DTL::Value->safe(undef) unless $Dotiac::DTL::ALLOW_METHOD_CALLS;
138 16         17 my $found=0;
139 16         64 foreach my $k ($param->dotiac_get_all_methods()) {
140 24 100       66 if (lc($k) eq $lcn) {
141 16         17 $found=1;
142 16         52 $param=$param->$k();
143 16         112 last;
144             }
145             }
146 16 50 33     44 if (not $found and $param->can("__getitem__")) {
147 0         0 my $x;
148 0 0       0 eval {
149 0         0 $x=$param->__getitem__($name);
150 0         0 1;
151             } or return Dotiac::DTL::Value->safe(undef);
152 0 0       0 if (defined $x) {
153 0         0 $param=$x;
154 0         0 next;
155             }
156             }
157 16 50       31 return Dotiac::DTL::Value->safe(undef) unless $found;
158 16         53 next;
159             }
160 0 0       0 return Dotiac::DTL::Value->safe($n) if $n!~/[^\d\-\.\,\e]/;
161 0         0 return Dotiac::DTL::Value->safe(undef);
162             }
163 32         200 return Dotiac::DTL::Value->new($param,!$escape);
164             }
165            
166            
167             package UNIVERSAL;
168            
169 1     1   8 use strict;
  1         2  
  1         98  
170            
171             sub dotiac_get_all_methods {
172 16     16 0 23 my ($class, undef) = @_;
173 16   33     39 $class = ref $class || $class;
174 16         18 my %classes_seen;
175             my %methods;
176 16         31 my @class = ($class);
177            
178 1     1   6 no strict 'refs';
  1         2  
  1         202  
179 16         37 while ($class = shift @class) {
180 16 50       111 next if $classes_seen{$class}++;
181 16         18 unshift @class, @{"${class}::ISA"};
  16         50  
182             # Based on methods_via() in perl5db.pl
183 16         17 for my $method (grep { # not /^[(_]/ and # Has to be removed, sadly
  64         303  
184 64         138 defined &{${"${class}::"}{$_}}}
  64         60  
  16         51  
185             keys %{"${class}::"}) {
186 32 50       643 $methods{$method} = wantarray ? undef : $class->can($method);
187             }
188             }
189            
190 16 50       76 wantarray ? keys %methods : \%methods;
191             }
192            
193             1;
194            
195             __END__