File Coverage

blib/lib/PHP/Include/Vars.pm
Criterion Covered Total %
statement 19 20 95.0
branch 1 2 50.0
condition n/a
subroutine 6 6 100.0
pod 0 1 0.0
total 26 29 89.6


line stmt bran cond sub pod time code
1             package PHP::Include::Vars;
2 8     8   4611 use warnings;
  8         13  
  8         227  
3 8     8   100 use strict;
  8         14  
  8         178  
4 8     8   38 use Filter::Simple;
  8         13  
  8         40  
5 8     8   15234 use Parse::RecDescent;
  8         215364  
  8         53  
6 8     8   8607 use Data::Dumper;
  8         60052  
  8         2617  
7              
8             our $perl = '';
9             our %declared_var = ();
10             our $QUALIFIER = 'my';
11              
12             sub declare {
13 67     67 0 501486 my $var = shift;
14 67 50       198 if ($declared_var{$var}) {
15 0         0 return $var
16             } else {
17 67         179 $declared_var{$var}++;
18 67         1593 return "$QUALIFIER $var"
19             }
20             }
21              
22             my $grammar = <<'GRAMMAR';
23              
24             php_vars: php_start statement(s) php_end
25              
26             php_start: /\s*<\?php\s*/
27              
28             php_end: /\s*\?>/
29              
30             statement: comment | assignment
31              
32             comment: /\s*(\#|\/\/).*/
33              
34             assignment: ( var_assign | hash_assign | array_assign | constant ) /;/
35             {
36             $PHP::Include::Vars::perl .= "$item[1];\n";
37             }
38              
39             var_assign: variable /=/ scalar
40             {
41             $return = PHP::Include::Vars::declare($item[1]) .
42             "=$item[3]";
43             }
44              
45             hash_assign: variable /=/ /Array\s*\(/i pair(s /,?/) /\s*(,\s*)?\)/
46             {
47             $item[1] =~ s/^\$/%/;
48             $return = PHP::Include::Vars::declare($item[1]) .
49             "=(" . join( ',', grep /./,@{$item[4]} ) . ')';
50             }
51              
52             array_assign: variable /=/ /Array\s*\(/i element(s /,?/) /\s*(,\s*)?\)/
53             {
54             $item[1] =~ s/^\$/@/;
55             $return = PHP::Include::Vars::declare($item[1]) .
56             "=(" . join( ',', grep /./,@{$item[4]} ) . ')';
57             }
58              
59             scalar: string | number
60              
61             variable: /\$[a-zA-Z_][0-9a-zA-Z_]*/
62              
63             number: /-?[0-9.]+/
64              
65             string: double_quoted | single_quoted
66              
67             double_quoted: / " (?: [^\\"] | \\" )* " /x
68              
69             single_quoted: /'.*?'/
70              
71             element: array | scalar | hash | bareword | comment { $return = "" }
72              
73             pair: scalar /=>/ ( scalar | array | hash | bareword )
74             {
75             $return = $item[1] . '=>' . $item[3];
76             }
77             | comment { $return = "" }
78              
79             array: /Array\s*\(/i element(s /,/) /\s*(,\s*)?\)/
80             {
81             $return = '['.join(',',@{$item[2]}) . ']';
82             }
83              
84             hash: /Array\s*\(/i pair(s /,/) /\s*(,\s*)?\)/
85             {
86             $return = '{'.join(',',@{$item[2]}) . '}';
87             }
88              
89             bareword: /[0-9a-zA-Z_]+/
90            
91             constant: /define\s*\(/ string /,/ scalar /\)/
92             {
93             $return = "use constant $item[2] => $item[4]";
94             }
95              
96             whitespace: /^\s+$/
97              
98             GRAMMAR
99              
100             my $parser = Parse::RecDescent->new( $grammar );
101              
102             FILTER {
103              
104             my ($class, $qualifier ) = map { lc($_) } @_;
105              
106             $QUALIFIER = $qualifier || "my";
107              
108             $perl = '';
109             # $::RD_TRACE = 1;
110             %declared_var = ();
111             $parser->php_vars( $_ );
112             print STDERR "\n\nGENERATED PERL:\n\n", $perl, "\n\n"
113             if $PHP::Include::DEBUG;
114             $_ = $perl;
115             }
116              
117             =head1 NAME
118              
119             PHP::Include::Vars
120              
121             =head1 SYNOPSIS
122              
123             use PHP::Include::Vars;
124            
125             $x = Array( 1,2,3 );
126             ?>
127             no PHP::Include::Vars;
128              
129             =head1 DESCRIPTION
130              
131             Please see PHP::Include for details. PHP::Include::Vars is an implementation
132             for the include_php_vars() macro.
133              
134             =head1 SEE ALSO
135              
136             =over 4
137              
138             =item * PHP::Include
139              
140             =item * Filter::Simple
141              
142             =item * Parse::RecDescent
143              
144             =back
145              
146             =head1 AUTHORS
147              
148             =over 4
149              
150             =item * Ed Summers
151              
152             =back
153              
154             =cut
155