home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / Include.pm < prev    next >
Encoding:
Perl POD Document  |  2002-12-31  |  4.1 KB  |  155 lines

  1. package PHP::Include;
  2.  
  3. use strict;
  4. use warnings;
  5. use Filter::Simple;
  6. use Carp qw( croak );
  7.  
  8. our $VERSION = .05;
  9. our $DEBUG = 0;
  10.  
  11. FILTER {
  12.  
  13.     ## read in any options and turn on diagnostics if asked 
  14.  
  15.     my ( $class, %options ) = map { lc($_) } @_; 
  16.     $DEBUG = 1 if $options{debug};
  17.  
  18.     ## include_php_vars() macro
  19.     s/
  20.     include_php_vars\s*\(\s*"(.+)"\s*\)\s*;
  21.     /
  22.     read_file( 'PHP::Include::Vars', $1)
  23.     /gex;
  24.  
  25. };
  26.  
  27. ## read a file and return it's contents with the appropriate
  28. ## filter around it
  29.  
  30. sub read_file {
  31.     my ($filter,$file) = @_;
  32.     print STDERR qq(OPENING PHP FILE "$file" FOR FILTER $filter\n\n) if $DEBUG;
  33.     open( IN, $file ) || croak( "$file doesn't exist!" );
  34.     my $php =  join( '', <IN> );
  35.     print STDERR "ORIGINAL PHP:\n\n", $php if $DEBUG;
  36.     close( IN );
  37.     return( "use $filter;\n" . $php . "no $filter;\n" );
  38. }
  39.  
  40. 1;
  41.  
  42. =head1 NAME
  43.  
  44. PHP::Include - Include PHP files in Perl 
  45.  
  46. =head1 SYNOPSIS
  47.  
  48.     use PHP::Include;
  49.     include_php_vars( 'file.php' );
  50.  
  51. =head1 DESCRIPTION
  52.  
  53. PHP::Include builds on the shoulders of Filter::Simple and Parse::RecDescent to
  54. provide a Perl utility for including very simple PHP Files from a Perl program.
  55.  
  56. When working with Perl and PHP it is often convenient to be able to share
  57. configuration data between programs written in both languages.  One solution to
  58. this would be to use a language independent configuration file (did I hear
  59. someone say XML?). Another solution is to use Perl's flexibility to read PHP
  60. and rewrite it as Perl. PHP::Include does the latter with the help of
  61. Filter::Simple and Parse::RecDescent to rewrite very simple PHP as Perl.
  62.  
  63. Filter::Simple is used to enable macros (at the moment only one) which 
  64. cause PHP to be interpolated into your Perl source code, which is then parsed
  65. using a Parse::RecDescent grammar to generate the appropriate Perl.
  66.  
  67. PHP::Include was designed to allow the more adventurous to add grammars that 
  68. extend the complexity of PHP that may be included. 
  69.  
  70. =head1 EXPORTS
  71.  
  72. =head2 import_php_vars( file )
  73.  
  74. This function is actually a macro that allows you to include PHP variable
  75. declarations in much the same way that you might C<require> a file of Perl 
  76. code. For example, given a file of PHP variable declarations:
  77.  
  78.     <?php
  79.  
  80.     define( "PORT", 80 );
  81.     $robot = 'Book Agent';
  82.     $hosts = Array( 
  83.     'www.amazon.com'    => 'Amazon',
  84.     'www.bn.com'        => 'Barnes and Noble',
  85.     'www.bookpool.com'    => 'BookPool'
  86.     );
  87.     $times = Array( 10,12,14,16,18 );
  88.  
  89.     ?>
  90.  
  91. You can use this from your Perl program like so:
  92.  
  93.     use PHP::Include;
  94.     include_php_vars( 'file.php' );
  95.  
  96. Behind the scenes the PHP is rewritten as this Perl:
  97.  
  98.     use constant PORT => 80;
  99.     my $robot = 'Book Agent';
  100.     my %hosts = (
  101.     'www.amazon.com'    => 'Amazon',
  102.     'www.bn.com'        => 'Barnes & Noble',
  103.     'www.bookpool.com'    => 'BookPool'
  104.     );
  105.     my @times = ( 10,12,14,16,18 );
  106.  
  107. Notice that the enclosing E<lt>php? and ?E<gt> are removed, all variables are 
  108. lexically scoped with 'my' and that the $ sigils are changed as appropriate to 
  109. (@ and %). In addition PHP constant definitions are translated into Perl 
  110. constants. 
  111.  
  112. =head1 DIAGNOSTICS
  113.  
  114. If you would like to see diagnostic information on STDERR you will need to 
  115. use this module slightly differently:
  116.  
  117.     use PHP::Include ( DEBUG => 1 );
  118.  
  119. This will cause the PHP that is read in, and the generated Perl to be printed on
  120. STDERR. It can be handy if you are trying to extend the grammar, or are trying
  121. to figure out what isn't getting parsed properly.
  122.  
  123. =head1 TODO
  124.  
  125. =over 4
  126.  
  127. =item * assigning directly to array elements 
  128.  
  129. =item * support other PHP code enclosures
  130.  
  131. =item * store compiled grammar if possible for speed gain
  132.  
  133. =head1 SEE ALSO
  134.  
  135. =over 4
  136.  
  137. =item * PHP::Include::Vars
  138.  
  139. =item * Filter::Simple
  140.  
  141. =item * Parse::RecDescent
  142.  
  143. =head1 AUTHOR
  144.  
  145. Ed Summers, E<lt>ehs@pobox.comE<gt>
  146.  
  147. =head1 COPYRIGHT AND LICENSE
  148.  
  149. Copyright 2002 by Ed Summers
  150.  
  151. This library is free software; you can redistribute it and/or modify
  152. it under the same terms as Perl itself. 
  153.  
  154. =cut
  155.