line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package PHP::Include; |
2
|
|
|
|
|
|
|
|
3
|
9
|
|
|
9
|
|
191714
|
use strict; |
|
9
|
|
|
|
|
23
|
|
|
9
|
|
|
|
|
222
|
|
4
|
9
|
|
|
9
|
|
44
|
use warnings; |
|
9
|
|
|
|
|
14
|
|
|
9
|
|
|
|
|
231
|
|
5
|
9
|
|
|
9
|
|
9516
|
use Filter::Simple; |
|
9
|
|
|
|
|
288547
|
|
|
9
|
|
|
|
|
58
|
|
6
|
9
|
|
|
9
|
|
449
|
use Carp qw( croak ); |
|
9
|
|
|
|
|
18
|
|
|
9
|
|
|
|
|
3634
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.37'; |
9
|
|
|
|
|
|
|
our $DEBUG = 0; |
10
|
|
|
|
|
|
|
our $QUALIFIER = "my"; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
FILTER { |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
## read in any options and turn on diagnostics if asked |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my ( $class, %options ) = map { lc($_) } @_; |
17
|
|
|
|
|
|
|
$DEBUG = 1 if $options{debug}; |
18
|
|
|
|
|
|
|
$QUALIFIER = "our" if $options{our}; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
## include_php_vars() macro |
21
|
|
|
|
|
|
|
s/ |
22
|
|
|
|
|
|
|
(.*) # any amt of leading text |
23
|
|
|
|
|
|
|
include_php_vars # function call |
24
|
|
|
|
|
|
|
\s* # optional whitespace |
25
|
|
|
|
|
|
|
\( # opening parens |
26
|
|
|
|
|
|
|
\s* # optional whitespace |
27
|
|
|
|
|
|
|
(["']) # opening single or double quote |
28
|
|
|
|
|
|
|
(.+) # a string |
29
|
|
|
|
|
|
|
\2 # closing single or double quote |
30
|
|
|
|
|
|
|
\s* # optional whitespace |
31
|
|
|
|
|
|
|
\) # closing paren |
32
|
|
|
|
|
|
|
.*; # rest of the line |
33
|
|
|
|
|
|
|
/ |
34
|
|
|
|
|
|
|
$1. |
35
|
|
|
|
|
|
|
read_file( 'PHP::Include::Vars', $3); |
36
|
|
|
|
|
|
|
/gex; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
}; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
## read a file and return it's contents with the appropriate |
43
|
|
|
|
|
|
|
## filter around it |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub read_file { |
46
|
8
|
|
|
8
|
0
|
28
|
my ($filter,$file) = @_; |
47
|
8
|
50
|
|
|
|
43
|
print STDERR qq(OPENING PHP FILE "$file" FOR FILTER $filter\n\n) if $DEBUG; |
48
|
8
|
50
|
|
|
|
282
|
open( IN, $file ) || croak( "$file doesn't exist!" ); |
49
|
8
|
|
|
|
|
214
|
my $php = join( '', ); |
50
|
|
|
|
|
|
|
# strip comments (sorry if you have # in strings) |
51
|
|
|
|
|
|
|
## $php =~ s!^\s*(?:#|//).*\n!!gm; # full line comments, delete line |
52
|
|
|
|
|
|
|
## $php =~ s!(?:#|//).*\n!\n!g; # others, keep new line |
53
|
8
|
50
|
|
|
|
137
|
print STDERR "ORIGINAL PHP:\n\n", $php if $DEBUG; |
54
|
8
|
|
|
|
|
60
|
close( IN ); |
55
|
8
|
|
|
|
|
126
|
return( "use $filter '$QUALIFIER';\n" . $php . "no $filter;\n" ); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
1; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=encoding UTF-8 |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 NAME |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
PHP::Include - Include PHP files in Perl |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 SYNOPSIS |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
use PHP::Include; |
69
|
|
|
|
|
|
|
include_php_vars( 'file.php' ); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head1 DESCRIPTION |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
PHP::Include builds on the shoulders of Filter::Simple and Parse::RecDescent to |
74
|
|
|
|
|
|
|
provide a Perl utility for including very simple PHP Files from a Perl program. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
When working with Perl and PHP it is often convenient to be able to share |
77
|
|
|
|
|
|
|
configuration data between programs written in both languages. One solution to |
78
|
|
|
|
|
|
|
this would be to use a language independent configuration file (did I hear |
79
|
|
|
|
|
|
|
someone say XML?). Another solution is to use Perl's flexibility to read PHP |
80
|
|
|
|
|
|
|
and rewrite it as Perl. PHP::Include does the latter with the help of |
81
|
|
|
|
|
|
|
Filter::Simple and Parse::RecDescent to rewrite very simple PHP as Perl. |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Filter::Simple is used to enable macros (at the moment only one) which |
84
|
|
|
|
|
|
|
cause PHP to be interpolated into your Perl source code, which is then parsed |
85
|
|
|
|
|
|
|
using a Parse::RecDescent grammar to generate the appropriate Perl. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
PHP::Include was designed to allow the more adventurous to add grammars that |
88
|
|
|
|
|
|
|
extend the complexity of PHP that may be included. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head1 EXPORTS |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head2 include_php_vars( file ) |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
This function is actually a macro that allows you to include PHP variable |
95
|
|
|
|
|
|
|
declarations in much the same way that you might C a file of Perl |
96
|
|
|
|
|
|
|
code. For example, given a file of PHP variable declarations: |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
define( "PORT", 80 ); |
101
|
|
|
|
|
|
|
$robot = 'Book Agent'; |
102
|
|
|
|
|
|
|
$hosts = Array( |
103
|
|
|
|
|
|
|
'www.amazon.com' => 'Amazon', |
104
|
|
|
|
|
|
|
'www.bn.com' => 'Barnes and Noble', |
105
|
|
|
|
|
|
|
'www.bookpool.com' => 'BookPool' |
106
|
|
|
|
|
|
|
); |
107
|
|
|
|
|
|
|
$times = Array( 10,12,14,16,18 ); |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
?> |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
You can use this from your Perl program like so: |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
use PHP::Include; |
114
|
|
|
|
|
|
|
include_php_vars( 'file.php' ); |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Behind the scenes the PHP is rewritten as this Perl: |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
use constant PORT => 80; |
119
|
|
|
|
|
|
|
my $robot = 'Book Agent'; |
120
|
|
|
|
|
|
|
my %hosts = ( |
121
|
|
|
|
|
|
|
'www.amazon.com' => 'Amazon', |
122
|
|
|
|
|
|
|
'www.bn.com' => 'Barnes & Noble', |
123
|
|
|
|
|
|
|
'www.bookpool.com' => 'BookPool' |
124
|
|
|
|
|
|
|
); |
125
|
|
|
|
|
|
|
my @times = ( 10,12,14,16,18 ); |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Notice that the enclosing Ephp? and ?E are removed, all |
128
|
|
|
|
|
|
|
variables are lexically scoped with 'my' and that the $ sigils are |
129
|
|
|
|
|
|
|
changed as appropriate to (@ and %). In addition PHP constant |
130
|
|
|
|
|
|
|
definitions are translated into Perl constants. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 MY vs OUR |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Variables are usually defined using 'my' qualifier. A 'our' qualifier |
135
|
|
|
|
|
|
|
can be forced using: |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
use PHP::Include ( our => 1 ); |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head1 DIAGNOSTICS |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
If you would like to see diagnostic information on STDERR you will |
142
|
|
|
|
|
|
|
need to use this module slightly differently: |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
use PHP::Include ( DEBUG => 1 ); |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
This will cause the PHP that is read in, and the generated Perl to be printed on |
147
|
|
|
|
|
|
|
STDERR. It can be handy if you are trying to extend the grammar, or are trying |
148
|
|
|
|
|
|
|
to figure out what isn't getting parsed properly. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 TODO |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=over 4 |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item * assigning directly to array elements |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=item * support other PHP code enclosures |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=item * store compiled grammar if possible for speed gain |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=back |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head1 SEE ALSO |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=over 4 |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=item * PHP::Include::Vars |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item * Filter::Simple |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=item * Parse::RecDescent |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=back |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head1 AUTHOR |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Maintained by Alberto Simões, Eambs@cpan.orgE |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Ed Summers, Eehs@pobox.comE |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
Copyright 2002-2010 by Ed Summers |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Copyright 2011-2013 by Alberto Simões |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
187
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=cut |