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 / cpp.pm < prev    next >
Encoding:
Perl POD Document  |  2003-09-17  |  2.2 KB  |  127 lines

  1. package Filter::cpp;
  2.  
  3. use Config ;
  4. use Carp ;
  5. use Filter::Util::Exec ;
  6. use strict;
  7. use warnings;
  8. use vars qw($VERSION);
  9.  
  10. $VERSION = '1.03' ;
  11.  
  12. my $cpp;
  13. my $sep;
  14. if ($^O eq 'MSWin32') {
  15.     $cpp = 'cpp.exe' ;
  16.     $sep = ';';
  17. }
  18. else {
  19.     ($cpp) = $Config{cppstdin} =~ /^(\S+)/;
  20.     $sep = ':';
  21. }
  22.  
  23. croak ("Cannot find cpp\n")
  24.     if ! $cpp;
  25.  
  26. # Check if cpp is installed
  27. if ( ! -x $cpp) {
  28.     my $foundCPP = 0 ;
  29.     foreach my $dir (split($sep, $ENV{PATH}), '')
  30.     {
  31.         if (-x "$dir/$cpp")
  32.         {
  33.             $foundCPP = 1;
  34.             last ;
  35.         }
  36.     }
  37.  
  38.     croak "Cannot find cpp\n"
  39.         if ! $foundCPP ;
  40. }
  41.  
  42. sub import 
  43.     my($self, @args) = @_ ;
  44.  
  45.     #require "Filter/exec.pm" ;
  46.  
  47.     if ($^O eq 'MSWin32') {
  48.         Filter::Util::Exec::filter_add ($self, 'cmd', '/c', 
  49.         "cpp.exe 2>nul") ;
  50.     }
  51.     else {
  52.         Filter::Util::Exec::filter_add ($self, 'sh', '-c', 
  53.         "$Config{'cppstdin'} $Config{'cppminus'} 2>/dev/null") ;
  54.     }
  55. }
  56.  
  57. 1 ;
  58. __END__
  59.  
  60. =head1 NAME
  61.  
  62. Filter::cpp - cpp source filter
  63.  
  64. =head1 SYNOPSIS
  65.  
  66.     use Filter::cpp ;
  67.  
  68. =head1 DESCRIPTION
  69.  
  70. This source filter pipes the current source file through the C
  71. pre-processor (cpp) if it is available.
  72.  
  73. As with all source filters its scope is limited to the current source
  74. file only. Every file you want to be processed by the filter must have a
  75.  
  76.     use Filter::cpp ;
  77.  
  78. near the top.
  79.  
  80. Here is an example script which uses the filter:
  81.  
  82.     use Filter::cpp ;
  83.  
  84.     #define FRED 1
  85.     $a = 2 + FRED ;
  86.     print "a = $a\n" ;
  87.     #ifdef FRED
  88.     print "Hello FRED\n" ;
  89.     #else
  90.     print "Where is FRED\n" ;
  91.     #endif
  92.  
  93. And here is what it will output:
  94.  
  95.     a = 3
  96.     Hello FRED
  97.  
  98. This example below, provided by Michael G Schwern, shows a clever way
  99. to get Perl to use a C pre-processor macro when the Filter::cpp module
  100. is available, or to use a Perl sub when it is not.
  101.  
  102.     # use Filter::cpp if we can.
  103.     BEGIN { eval 'use Filter::cpp' }
  104.  
  105.     sub PRINT {
  106.         my($string) = shift;
  107.  
  108.     #define PRINT($string) \
  109.         (print $string."\n")
  110.     }
  111.  
  112.     PRINT("Mu");
  113.  
  114. Look at Michael's Tie::VecArray module for a practical use.
  115.  
  116. =head1 AUTHOR
  117.  
  118. Paul Marquess
  119.  
  120. =head1 DATE
  121.  
  122. 11th December 1995.
  123.  
  124. =cut
  125.  
  126.