in reply to Multi-line comments in perl code?
I think it's useful to boil down what's already been said or referred to.
- POD is the official way to do multi line comments in Perl, whether you think of it as as a work-around or not (which varies),
- use "=for comment" or "=begin/end comment" for stuff that's just comments and shouldn't show up in the output text when documentation is extracted from the code, such as when you run perldoc on the program,
- You can't indent the "=for comment", so it's not quite like other language's multiline comments, and
- You do need a "=cut" at the end to get back to code. (I think this might be your main objection to using POD, and, yes, you're stuck with it).
The last item may change with Perl6, for "=begin/end", but the rest will probably not. See Apocalypse 2 RFC 5
Here's an example which you can runn with "perl" and with "perldoc" to see what shows up where. Note that in many places, such as before the "=cut", the blank line is part of the syntax.
=head1 tstcmt.pl
This is text that shows up with the "perldoc tstcmt.pl"
command. The comment sections below do not.
=cut
print "hello\n";
=for comment
This is a single paragraph of comment text, spanning
multiple lines. It ends with the first blank line.
After that, the text is inuded with other printed pod text
that shows up with perldoc
This text also shows up with perldoc, it's not part of
the "for" comment above.
=cut
print "hello1\n";
=for comment
Another paragraph of comment text, spanning
multiple lines. It ends with the first blank line.
=cut
print "hello2\n";
=begin comment
This is a multi-paragraph comment section.
Which is terminated by an =end comment on a line by itself.
This is a second paragraph
=end comment
=cut
print "hello3\n";
=head2 Other pod documentation
This is text that also shows up with the "perldoc tstcmt.pl"
command. The comment sections above do not.
=cut
print "hello4\n"
Re^2: Multi-line comments in perl code?
by TheDamian (Vicar) on Jul 14, 2006 at 13:33 UTC
|
The last item may change with Perl6, for "=begin/end", but the rest will probably not.
Actually, the first point:
POD is the official way to do multi line comments in Perl
will also change in Perl 6, which introduces an indentable, nestable, multi-line-able delimited comment:
#{ comment
here
}
#[ comment
here
]
#( comment here )
#< comment here >
#« comment etc. »
A delimited comment is introduced by a # followed immediately by any kind of opening bracket. It is closed by the corresponding closing bracket. | [reply] [d/l] [select] |
|
#<<<
#<{[
because I could easily see myself wanting to comment out blocks of code with unbalanced closing brackets of all four types (or, more to the point, a dynamic selection of closing bracket types). Having an unlimited variety of comment "identifiers" also facilitates arbitrarily nesting comments without artificial restrictions.
We're building the house of the future together.
| [reply] |
|
Ow. Would it be too much to ask that it accept any sequence of opening brackets
Not to much to ask at all. At least, not as long as you can be satisfied with multiple repetitions of the same delimiter character. From the latest update of Synopsis 2:
| [reply] |
|
|
| [reply] |
|
Yay!++
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
Re^2: Multi-line comments in perl code?
by Anonymous Monk on Jul 13, 2006 at 17:58 UTC
|
Also you can always look at another Perl module and see how they document their code :) | [reply] |
Re^2: Multi-line comments in perl code?
by cxw (Scribe) on Apr 03, 2020 at 01:29 UTC
|
To clarify: as of writing, on Perl 5.28.1 and Perldoc v3.2801:
- =for comment hides text from perl but not perldoc
- =begin comment hides text from both perl and perldoc
For example, given foo.pl as follows:
=head1 SECTION
=for comment
Comment!
=cut
=begin comment
Comment! Number 2
=end comment
=cut
Therefore, I personally use =begin comment / =end comment / =cut, even though it's a bit tedious.
| [reply] [d/l] [select] |
|
=for comment
Comment!
=cut
... produces:
SECTION
Comment!
That is the correct behavior, your conclusion "=for comment hides text from perl but not perldoc" isn't correct. From perlpod (emphasis mine):
The command "=for formatname text..." specifies that the remainder of just this paragraph (starting right after formatname) is in that special format.
Remove the blank line after the =for and the "Comment!" is hidden. The =for still begins a section of POD that needs to be terminated by =cut, so Perl will ignore the entire example you posted.
| [reply] [d/l] [select] |
Re^2: Multi-line comments in perl code?
by aamailhot (Initiate) on Jan 04, 2017 at 20:36 UTC
|
It seems that the '=begin comment'/'=end comment' pair doesn't work inside of if-blocks, does anyone know if there is a fix for it?
E.g. the following code will fail to compile with a 'missing right curly' syntax error.
if(1==1) {
=begin comment
print "something else\n";
=end comment
print "hello!\n";
}
| [reply] [d/l] |
|
You're missing a "=cut" to end the POD.
| [reply] |
|
|