tags:

views:

225

answers:

4

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

foo = """line1
line2
line3"""

Is there something equivalent in Perl?

+11  A: 

Yes, a here-doc.

$heredoc = <<END;
Some multiline
text and stuff
END
Steve
+12  A: 

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";
ire_and_curses
While this is correct, you really want to look at here-docs. See the other answers.
tsee
@tsee: Why? What's the advantage of a here-doc to the OP's simple string problem? Here-docs have the major disadvantage of compromising the indentation of your code (they have to be left-justified).
ire_and_curses
So does the ordinary quote unless you want extra whitespace at the end. here-docs are preferable simply because ' and " are shitty identifiers for scanning for the end of a very long string.
tsee
+16  A: 

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_TEXT

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

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
};
daotoad
A: 
oren
Welcome to Stack Overflow. If you're going to provide an answer to a question that already has several good answers, especially a question for which one answer has already been accepted, you should consider whether your answer actually adds any new information. In this case, yours does not. Also, when you're writing an answer, please pay attention to the preview below the edit box; it would have shown you that your answer doesn't really illustrate multiline strings very well. Refer to the formatting instructions to the right of the edit box.
Rob Kennedy