134

Possible Duplicate:
What are the common workarounds for multi-line comments in Perl?

How do I add a multi-line comment to Perl source code?

Community
  • 1
  • 1
vrbilgi
  • 5,703
  • 12
  • 44
  • 60

2 Answers2

165

POD is the official way to do multi line comments in Perl. See:

The quick-and-dirty way to comment out more than one line of Perl is to surround those lines with Pod directives. You have to put these directives at the beginning of the line and somewhere where Perl expects a new statement (so not in the middle of statements like the # comments). You end the comment with =cut, ending the Pod section:

=pod

my $object = NotGonnaHappen->new();

ignored_sub();

$wont_be_assigned = 37;

=cut

The quick-and-dirty method only works well when you don't plan to leave the commented code in the source. If a Pod parser comes along, your multiline comment is going to show up in the Pod translation. A better way hides it from Pod parsers as well.

The =begin directive can mark a section for a particular purpose. If the Pod parser doesn't want to handle it, it just ignores it. Label the comments with comment. End the comment using =end with the same label. You still need the =cut to go back to Perl code from the Pod comment:

=begin comment

my $object = NotGonnaHappen->new();

ignored_sub();

$wont_be_assigned = 37;

=end comment

=cut
brian d foy
  • 129,424
  • 31
  • 207
  • 592
Nikhil Jain
  • 8,232
  • 2
  • 25
  • 47
  • 7
    It isn't necessary to start the comment with =POD, you can use any thing to start Multi-line comment ( say =xyz etc ) But yes end must always be with =cut not even =CUT – Bharat Pahalwani Jun 13 '14 at 04:44
31

I found it. Perl has multi-line comments:

#!/usr/bin/perl

use strict;

use warnings;

=for comment

Example of multiline comment.

Example of multiline comment.

=cut

print "Multi Line Comment Example \n";
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vrbilgi
  • 5,703
  • 12
  • 44
  • 60
  • 7
    Between `=for comment` and `=cut`, from the second paragraph, it will be shown in *perldoc*. So only the first paragraph would be fully commented from both codes and *perldoc*. If this should be avoided, use `=begin comment` ...multi-lines/paragraph comments... `=end comment (*new-line*) =cut`. – Andrew_1510 Oct 15 '12 at 03:26