Filter::cpp(3pm)

✨ Filter::cpp β€” cpp source filter ✨

okay so like. bestie. this is Filter::cpp, a cpp source filter, and it's genuinely so much fun. it's a Perl module from the User Contributed Perl Documentation crew, and it's giving "let me sneak the C pre-processor into your Perl script" energy πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

use Filter::cpp ;

that's it. that's the whole invite. one line and you're in 🫢


🌸 DESCRIPTION 🌸

so here's the tea: this source filter pipes the current source file through the C pre-processor (cpp) β€” if it is available. if cpp isn't around, no slay for you, sorry babe 😭

and like every source filter ever, its scope is limited to the current source file only. it does not spread. it does not travel. every single file you want processed by the filter has to have its own

use Filter::cpp ;

near the top. yes, every file. no shortcuts, no cap πŸ‘‘


here's an example script that uses the filter, look at her go:

use Filter::cpp ;

#define FRED 1
$a = 2 + FRED ;
print "a = $a\n" ;
#ifdef FRED
print "Hello FRED\n" ;
#else
print "Where is FRED\n" ;
#endif

and here's what it outputs, iconic behaviour honestly:

a = 3
Hello FRED

see?? FRED got pre-processed into 1, 2 + 1 is 3, and the #ifdef did its little conditional moment and picked the Hello FRED branch. the #else never got a turn. rip to Where is FRED πŸ’€


and okay, this next example is the moment. it was provided by Michael G Schwern, and it 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")
}

PRINT("Mu");

the way that BEGIN { eval 'use Filter::cpp' } just tries it and gracefully moves on if it can't?? main character energy fr fr πŸ™ˆ both worlds, one script, no drama.

if you want a practical use of all this, go look at Michael's Tie::VecArray module. that's where the vibe gets real πŸ’–


πŸ‘‘ AUTHOR πŸ‘‘

Paul Marquess β€” the icon, the legend, the one who did all of this ✨


πŸŽ€ DATE πŸŽ€

11th December 1995. β€” a true vintage serve, and it still hits 🌈


and that's the whole page bestie!! go forth and pre-process πŸ¦‹πŸ’œ

mwah β€” with love from the User Contributed Perl Documentation, page Filter::cpp(3pm), shipped with perl v5.42.3, dated 2026-08-19. iykyk 🫢🌸

πŸ“¦ where this page comes from