Search code examples
perlcomments

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


This RFC mentions

Unlike many programming languages Perl does not currently implement true multiline comments. This, and the workarounds that are in common use can be problematic. This could be solved by adding a new syntax to allow for comments to span more than one line, like the variation on here-documentation cited below.

What are the common workarounds?

Two techniques I found here are

if (0) {
  <comment>
}

and

=pod
<comment>
=cut

Are these safe to use? Are there others that work better?


Solution

  • The downside of the "if" solution is that the commented out code still has to be compiled (and therefore still has to be syntax checked).

    The downside of your pod solution is that your comments will appear in any documentation generated from the pod.

    I use a version of the pod solution that doesn't have that problem. Pod supports =begin format ... =end format paragraphs that are handled by specific formatters. I just invent a "comment" format that isn't handled by any of the formatters I use.

    #!/usr/bin/perl
    
    print "This line is executed\n";
    
    =begin comment
    
    print "This line isn't\n";
    
    =end comment
    
    =cut
    
    print "This line is\n";