Date Created: 2025-05-20
By: 16BitMiker
[ BACK.. ]
Perl is well known for its powerful text processing capabilities, and few features showcase its flexibility better than its regular expression engine. While most Perl developers reach for standard HEREDOC syntax to insert multiline text, there are times when indentation constraints or stylistic preferences prompt us to look for alternatives.
In this post, we’ll explore a creative (and slightly unconventional) way to embed and clean up multiline strings using only Perl’s regex substitution operators. This technique is not meant to replace HEREDOCs entirely, but rather to demonstrate just how expressive and flexible Perl's PCRE (Perl Compatible Regular Expressions) can be when you think outside the box. 🧠
Let’s look at the complete example:
xxxxxxxxxx
#!/usr/bin/env perl
# Define a subroutine that returns a cleaned multiline string
sub
{
# Declare an undefined scalar
my $text;
# The first substitution inserts a multiline string into $text
# The second substitution cleans it up by removing leading whitespace and empty lines
return $text =~ s~^~
hey how are you?
I am great!
~ =~ s~^\s+|^$|\n{2,}~~mg ;
}
# Call the subroutine and assign the output to a variable
my $text = ();
# Print the cleaned string
print $text;
xxxxxxxxxx
hey how are you?
I am great!
Clean, legible, and no HEREDOC tags in sight. Let’s break it down.
xxxxxxxxxx
$text =~ s~^~
hey how are you?
I am great!
~
$text
is initially undef
, but we still use the substitution operator s///r
.
The pattern s~^~...~r
matches the beginning of the string and replaces it with our multiline block.
The r
modifier tells Perl to return the result of the substitution instead of modifying $text
itself.
This effectively "injects" the multiline string into the scalar.
xxxxxxxxxx
=~ s~^\s+|^$|\n{2,}~~mg ;
This chained substitution applies several filters:
^\s+
— removes leading whitespace from each line (thanks to the m
modifier for multiline mode).
^$
— removes completely empty lines.
\n{2,}
— ensures no more than one newline in a row.
g
— applies the substitution globally across the entire string.
r
— returns the cleaned string without modifying the intermediate result.
Because Perl allows us to chain substitutions, we can:
Inject a block of text at runtime.
Immediately clean and format it in-place.
Return the result as a value.
And all of this works inside an indented subroutine block — no need to break formatting or manage HEREDOC markers at the start of lines.
This example isn’t just a cool trick — it highlights how expressive Perl's regex system is. Regular expressions are often thought of as search tools, but in Perl, they can become full-blown text constructors and cleaners.
While this technique may not suit every situation (especially when working with extremely large or user-generated text blocks), it’s an excellent illustration of the kind of creative problem-solving that Perl encourages. ⚙️
Consider using this approach when:
You want to avoid HEREDOC syntax in deeply indented code.
You’re embedding small, controlled snippets of text.
You want an elegant one-liner-style solution wrapped in a subroutine.
You enjoy the expressive power of Perl’s regex engine and want to flex it a bit.
This "rexdoc" pattern may seem unorthodox, but it’s a great example of Perl’s flexibility and regex creativity. Whether you're trying to keep your code tidy or you just enjoy bending syntax to your will, Perl gives you plenty of rope — and the tools to tie it into elegant knots. 🪢
So next time you reach for a HEREDOC, remember: with Perl, there’s always another way.