Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

foo = """line1 line2 line3""" 

Is there something equivalent in Perl?

like image 376
Mike Avatar asked May 20 '10 18:05

Mike


People also ask

How do I print multiple lines in Perl?

User can create a multiline string using the single(”) quotes and as well as with double quotes(“”). Using double quotes cause variables embedded in the string to be replaced by their content while in single quotes variables name remained the same.

What is multiline string in Python?

A multiline string in Python begins and ends with either three single quotes or three double quotes. Any quotes, tabs, or newlines in between the “triple quotes” are considered part of the string. Python's indentation rules for blocks do not apply to lines inside a multiline string.

Which symbol is used for multiline strings in Python?

Use triple quotes to create a multiline string Anything inside the enclosing Triple quotes will become part of one multiline string. Let's have an example to illustrate this behavior. # String containing newline characters line_str = "I'm learning Python.


2 Answers

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.

like image 163
daotoad Avatar answered Sep 20 '22 05:09

daotoad


Perl doesn't have significant syntactical vertical whitespace, so you can just do

$foo = "line1 line2 line3 "; 

which is equivalent to

$foo = "line1\nline2\nline3\n"; 
like image 36
ire_and_curses Avatar answered Sep 19 '22 05:09

ire_and_curses