Does Perl have the equivalent of Python's multiline strings?

Mike picture Mike · May 20, 2010 · Viewed 20.6k times · Source

In Python you can have a multiline string like this using a docstring

foo = """line1
line2
line3"""

Is there something equivalent in Perl?

Answer

daotoad picture daotoad · May 20, 2010

Normal quotes:

# Non-interpolative
my $f = 'line1
line2
line3
';

# Interpolative
my $g = "line1
line2
line3
";

Here-docs allow you to define any token as the end of a block of quoted text:

# Non-interpolative
my $h = <<'END_TXT';
line1
line2
line3
END_TXT

# Interpolative
my $h = <<"END_TXT";
line1
line2
line3
END_TXT

Regex style quote operators let you use pretty much any character as the delimiter--in the same way a regex allows you to change delimiters.

# Non-interpolative
my $i = q/line1
line2
line3
/;

# Interpolative
my $i = qq{line1
line2
line3
};

UPDATE: Corrected the here-doc tokens.