File Coverage

blib/lib/PHP/Include.pm
Criterion Covered Total %
statement 19 19 100.0
branch 3 6 50.0
condition n/a
subroutine 5 5 100.0
pod 0 1 0.0
total 27 31 87.1


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