Add the following packages libalgorithm-diff-perl libspiffy-perl libtext-diff-perl...
[pkg-perl] / deb-src / libfilter-perl / libfilter-perl-1.34 / lib / Filter / cpp.pm
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
44     my($self, @args) = @_ ;
45
46     #require "Filter/exec.pm" ;
47
48     if ($^O eq 'MSWin32') {
49         Filter::Util::Exec::filter_add ($self, 'cmd', '/c', 
50                 "cpp.exe 2>nul") ;
51     }
52     else {
53         Filter::Util::Exec::filter_add ($self, 'sh', '-c', 
54                 "$Config{'cppstdin'} $Config{'cppminus'} 2>/dev/null") ;
55     }
56 }
57
58 1 ;
59 __END__
60
61 =head1 NAME
62
63 Filter::cpp - cpp source filter
64
65 =head1 SYNOPSIS
66
67     use Filter::cpp ;
68
69 =head1 DESCRIPTION
70
71 This source filter pipes the current source file through the C
72 pre-processor (cpp) if it is available.
73
74 As with all source filters its scope is limited to the current source
75 file only. Every file you want to be processed by the filter must have a
76
77     use Filter::cpp ;
78
79 near the top.
80
81 Here is an example script which uses the filter:
82
83     use Filter::cpp ;
84
85     #define FRED 1
86     $a = 2 + FRED ;
87     print "a = $a\n" ;
88     #ifdef FRED
89     print "Hello FRED\n" ;
90     #else
91     print "Where is FRED\n" ;
92     #endif
93
94 And here is what it will output:
95
96     a = 3
97     Hello FRED
98
99 This example below, provided by Michael G Schwern, shows a clever way
100 to get Perl to use a C pre-processor macro when the Filter::cpp module
101 is available, or to use a Perl sub when it is not.
102
103     # use Filter::cpp if we can.
104     BEGIN { eval 'use Filter::cpp' }
105
106     sub PRINT {
107         my($string) = shift;
108
109     #define PRINT($string) \
110         (print $string."\n")
111     }
112      
113     PRINT("Mu");
114
115 Look at Michael's Tie::VecArray module for a practical use.
116
117 =head1 AUTHOR
118
119 Paul Marquess 
120
121 =head1 DATE
122
123 11th December 1995.
124
125 =cut
126