Scroll to navigation

Filter::cpp(3pm) User Contributed Perl Documentation Filter::cpp(3pm)


Filter::cpp - cpp source filter


    use Filter::cpp ;


This source filter pipes the current source file through the C pre-processor (cpp) if it is available.

As with all source filters its scope is limited to the current source file only. Every file you want to be processed by the filter must have a

    use Filter::cpp ;

near the top.

Here is an example script which uses the filter:

    use Filter::cpp ;
    #define FRED 1
    $a = 2 + FRED ;
    print "a = $a\n" ;
    #ifdef FRED
    print "Hello FRED\n" ;
    print "Where is FRED\n" ;

And here is what it will output:

    a = 3
    Hello FRED

This example below, provided by Michael G Schwern, shows a clever way to get Perl to use a C pre-processor macro when the Filter::cpp module is available, or to use a Perl sub when it is not.

    # use Filter::cpp if we can.
    BEGIN { eval 'use Filter::cpp' }
    sub PRINT {
        my($string) = shift;
    #define PRINT($string) \
        (print $string."\n")

Look at Michael's Tie::VecArray module for a practical use.


Paul Marquess


11th December 1995.

2024-03-07 perl v5.38.2