Filter::m4(3pm)

✨ NAME ✨

Filter::m4 β€” the M4 source filter, bestie πŸ’–


πŸ¦‹ SYNOPSIS πŸ¦‹

use Filter::m4;
use Filter::m4 'prefix';

🌸 DESCRIPTION 🌸

Okay so here's the tea β˜•: this source filter takes your current source file and pipes it straight through the M4 macro processor (aka m4) β€” but only if it's available. No cap.

And like every other source filter out there, its scope is limited to the current source file only. So every single file you want the filter to touch needs this line chillin' near the top:

use Filter::m4;

That's the vibe check. Miss it and the file just... doesn't get processed. πŸ™ˆ


πŸŽ€ EXAMPLE πŸŽ€

Here's a lil baby example showing how to define an M4 macro and actually use it, because slay:

use Filter::m4;

define(`foo', `$1 =~ s/bar/baz/r')

$a = "foobar";
print "a = " . foo(`$a') . "\n";

And the output of the above example? It's giving ✨:

a = foobaz

πŸ’… NOTES πŸ’…

By default, M4 uses ` and ' as its quotes β€” iconic, but also: this is configurable using M4's changequote builtin. So you're not stuck with it, periodt.

M4 uses $1, $2, etc., to indicate arguments inside macros. And the way those clash with Perl regex syntax?? 😭 To avoid the drama, it's recommended to use Perl's alternative forms "${1}", "${1}", etc.

Also, heads up: the following keywords in M4 and Perl are identical, which is a whole situation:

eval
format
index
mkstemp
shift
substr

So if you need such keywords in your Perl code, you've gotta pick one of these three solutions, no skips:

  • Protect the keyword with M4 quotes, for example `shift'.
  • Redefine the problematic M4 builtin using defn, as outlined in section Renaming macros of the M4 info manual.
  • Use the "prefix" option. This adds the prefix m4_ to all M4 builtins (but not to user-defined macros) β€” so you'll have to use m4_shift instead of shift. Lowkey genius 🧚
Note that use Filter::m4 'prefix'; is the only import option here, so if you're doing prefix mode, that's the whole move.

πŸ‘‘ AUTHOR πŸ‘‘

Werner Lemberg β€” thank you for your service 🫢


⭐ DATE ⭐

17th March 2018.


And that's a wrap, bestie πŸ’œ This page came to you from perl v5.42.3, dated 2026-08-19, and it lives in the Filter::m4(3pm) manual. Byeee πŸŒˆπŸŽ€

πŸ“¦ where this page comes from