28

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

foo = """line1
line2
line3"""

Is there something equivalent in Perl?

ire_and_curses
  • 68,372
  • 23
  • 116
  • 141
Mike
  • 58,961
  • 76
  • 175
  • 221
  • 6
    Note that what you are doing is not a docstring. It's simply the multiline string syntax. Docstrings in general are strings at the beginning of classes or functions that are meant to explain the class/function and can be accessed via `name.__doc__`. Those are also used for automatical help page generation. – poke May 20 '10 at 18:42
  • 6
    Indeed, the nearest equivalent in Perl to actual Python docstrings would be Perl's "Plain Old Documentation" (http://perldoc.perl.org/perlpod.html), which is simultaneously more expressive and a few degrees more complex. I don't know if it provides the same ability to be gotten at programatically from the script it's used in, though. – cikkle May 20 '10 at 20:40
  • 1
    @cikkle, POD isn't available at runtime. – daotoad May 20 '10 at 21:30
  • daotad: Maybe not easily, but if deemed useful, coming up with something that uses `*PackageName::Data` and a POD parser to great mischief should be quite possible. But you're certainly right to point out that POD isn't included in any ordinary data structure by the compiler. – tsee May 21 '10 at 06:45

5 Answers5

46

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.

hepcat72
  • 890
  • 4
  • 22
daotoad
  • 26,689
  • 7
  • 59
  • 100
37

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
  • 68,372
  • 23
  • 116
  • 141
  • 3
    While this is correct, you really want to look at here-docs. See the other answers. – tsee May 21 '10 at 06:45
  • 4
    @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 May 21 '10 at 13:24
  • 4
    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 May 25 '10 at 07:39
  • 2
    @ire_and_curses Indented here-docs `<<~EOF` address this problem (available since Perl5 v26) – amon Jan 21 '19 at 14:22
17

Yes, a here-doc.

$heredoc = <<END;
Some multiline
text and stuff
END
Steve
  • 2,207
  • 6
  • 24
  • 35
0

Yes you have 2 options :

1.heredocs please note that every data in heredocs are interpolated :

my $data =<<END 

your data 

END

2.qq() see for example :

print qq(
 HTML

 $your text

 BODY

 HTML
);
Hameed
  • 2,227
  • 1
  • 21
  • 31
oren
  • 479
  • 1
  • 6
  • 10
  • 10
    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 May 21 '10 at 19:06
0

Quick example

#!/usr/bin/perl
use strict;
use warnings;

my $name = 'Foo';

my $message = <<'END_MESSAGE';
Dear $name,

this is a message I plan to send to you.

regards
  the Perl Maven
END_MESSAGE

print $message;

...Result:

Dear $name,

this is a message I plan to send to you.

regards
  the Perl Maven

Reference: http://perlmaven.com/here-documents

M Somerville
  • 4,499
  • 30
  • 38
Cristian
  • 548
  • 6
  • 8