Go to content Go to navigation Go to search

Multi-line Strings · Dec 19, 06:09 PM by Dylan Doxey

So, there you are, writing a script which sends an email to each of the staff wishing them each a nice day. To ensure that you are having a nice day also, you want your code to look nice and be maintainable by the next guy that comes along to change, debug or fix it later.


my $name = 'Mr. Pibb';

my $message = <<TEMPLATE;
Dear $name,

please have a nice day.

Thank you.

Sincerly, the Management.
TEMPLATE

Well, here's the thing... I personally don't like that syntax for doing multi-line strings. Yet of all the languages I've worked with, this solution offered by Perl seems to be the best thing going. (There is the possibility of keeping the text of the message in a template file to be read an processed without mucking up your code layout. But that's for a different post.)

Pros: Cons:

So, you might be inclined to ask what I'd suggest as an alternative.

my $message = "Dear $name,\n\nplease have a nice day.\n\nThank you.\n\nSincerly, the Management.";

This is the obvious choice.

Pros: Cons:

Let's try breaking it up to make the individual lines stand out.

my $message = "Dear $name,\n\n"
    . "please have a nice day.\n\n"
    . "Thank you.\n\n"
    . "Sincerly, the Management.";

Here's a big improvement over just jamming it all together in a single line.

Pros: Cons:

Let's try something that will better suggest the rectangularity of the intended result.

my $message = "Dear $name,"             . "\n"
            . ""                        . "\n"
            . "please have a nice day." . "\n"
            . ""                        . "\n"
            . "Thank you."              . "\n"
            . ""                        . "\n"
            . "Sincerly, the Management.";

Here's getting a little closer to my vision for a clean multi-line string definition.

Pros: Cons:

How about something more structured than a sequence of concatenated strings.

my $message = join "\n", (
    "Dear $name,               ",
    "                          ",
    "please have a nice day.   ",
    "                          ",
    "Thank you.                ",
    "                          ",
    "Sincerly, the Management. ",
);

Whoa, now we're getting somewhere. This gives us a strong sense of the shape and layout of the target document without compromising our code layout at all.

Pros: Cons:
my $message = join "\n", map { $_ =~ s/(\A [\s]* | [\s]* \z)//msxg } (
    "Dear $name,               ",
    "                          ",
    "please have a nice day.   ",
    "                          ",
    "Thank you.                ",
    "                          ",
    "Sincerly, the Management. ",
);

Nice.

Pros: Cons:
my $message = multiline_string(
    "Dear $name,               ",
    "                          ",
    "please have a nice day.   ",
    "                          ",
    "Thank you.                ",
    "                          ",
    "Sincerly, the Management. ",
);

sub multiline_string {
    return join "\n", map { $_ =~ s/(\A [\s]* | [\s]* \z)//msxg } @_;
}

Better.

Pros: Cons:
my $message =
    +----------------------------+
    | Dear $name,                |
    |                            |
    | please have a nice day.    |
    |                            |
    | Thank you.                 |
    |                            |
    | Sincerly, the Management.  |
    +----------------------------+
;

Now you're talking!
This is a snippet out of my fantasy language.

Pros: Cons:
my $message =
    +----------------------------+
    | Dear $name,                
    |                            
    | please have a nice day.    
    |                            
    | Thank you.                 
    |                            
    | Sincerly, the Management.  
    +----------------------------+
;

Perhaps this variation could suggest no trailing white-space.



UPDATE: Now see Filter::BoxString on CPAN for my implementation of this idea.

Commenting is closed for this article.