Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Is it safe/recommended to use pairs of =cut for multi line comments?

by Perl300 (Friar)
on Jun 10, 2016 at 17:02 UTC ( [id://1165314]=perlquestion: print w/replies, xml ) Need Help??

Perl300 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I was finding ways to use multi line comments (v5.22.0) and found perlfaq7 section "How can I comment out a large block of Perl code?" The two ways suggested there are:

=pod . . =cut
And
=begin comment . . . =end comment =cut

But when I was playing with it I noticed that using pairs of "=cut" also does the same without any warning/error, with use strict and warnings.

=cut . . . =cut

But I am not sure if it's safe or recommended as perlpodspec section "=cut" says that:

It is an error to try to start a Pod block with a "=cut" command. In that case, the Pod processor must halt parsing of the input file, and must by default emit a warning.

Also Excess =cut breaks module has some views against using pair of =cut for multi line commenting.

So I was hoping to get some views on if it's recommended or safe to use pairs of "=cut" for multi line commenting in production code? I felt pairs of "=cut" is easier to remember than "=pod" or "=begin/end comment".

I am using following simple test code for trying this out:

#!/opt/apps/perl/perl5220/bin/perl use strict; use warnings; print "Line1\n"; print "Line2\n"; =cut print "Line3\n"; =cut print "Line4\n"; =cut print "Line5\n"; =cut print "Line6\n";

Replies are listed 'Best First'.
Re: Is it safe/recommended to use pairs of =cut for multi line comments?
by haukex (Archbishop) on Jun 10, 2016 at 17:09 UTC

    Hi Perl300,

    Since the specification says not to begin a POD block with =cut, don't do it! :-) You never know if it might start breaking in a future version of Perl or with a different POD parser.

    Update: Also, podchecker complains if you start a POD block with =cut.

    Personally I'd use =begin comment with =end comment, or =for comment ..., since those are self-documenting.

    Hope this helps,
    -- Hauke D

Re: Is it safe/recommended to use pairs of =cut for multi line comments?
by LanX (Saint) on Jun 10, 2016 at 17:16 UTC
    Relying on undocumented behaviour is not only dangerous for compiling in future versions (like Hauke pointed out), you'll also sabotage many external tools trying to parse your code.

    For instance, I once had to edit code where =h1 (and so on) was used instead of =head1 , guess how well the syntax highlighting reacted with an not so forgiving editor?

    And last but not least many (all?) podparser should fail, do you really want to rule out any documentation?

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re: Is it safe/recommended to use pairs of =cut for multi line comments? [No: it's a syntax error]
by kcott (Archbishop) on Jun 10, 2016 at 21:30 UTC

    G'day Perl300,

    I would strongly recommend not using pairs of =cut commands. It raises pod syntax errors (which you've already identified in perlpodspec: =cut but also see below). A further issue, if you were ignoring the syntax errors, is that it makes your code very difficult to read. I'd probably choose something like:

    # ... some code here ... =for comment ... comments ... =cut # ... some more code here ...

    Notice the blank lines. Most POD commands need blank lines before and/or after them. perlpod: =cut has:

    "To end a Pod block, use a blank line, then a line beginning with "=cut", and a blank line after it. This lets Perl (and the Pod formatter) know that this is where Perl code is resuming. (The blank line before the "=cut" is not technically necessary, but many older Pod processors require it.)"

    If you're not already familiar with it, take a look at the podchecker utility.

    [podchecker is part of the core distribution and should already be installed. It's probably in the same directory as your perl executable.]

    It will tell you about problems with blank lines, e.g.

    $ podchecker podchecker_test_4.txt *** ERROR: Apparent command =head2 not preceded by blank line at line +7 in file podchecker_test_4.txt podchecker_test_4.txt has 1 pod syntax error.
    $ cat podchecker_test_4.txt #!/usr/bin/env perl use strict; use warnings; =head1 heading 1 =head2 heading 1.2

    And various other syntax errors, e.g.

    $ podchecker podchecker_test_5.txt *** ERROR: =over on line 6 without closing =back at line EOF in file p +odchecker_test_5.txt podchecker_test_5.txt has 1 pod syntax error.
    cat podchecker_test_5.txt #!/usr/bin/env perl use strict; use warnings; =over 4 whatever

    I recommend you make liberal use of this utility while you're learning to code POD. Here's what I got with the code you posted:

    $ podchecker podchecker_test_1.txt podchecker_test_1.txt does not contain any pod commands.
    cat podchecker_test_1.txt #!/opt/apps/perl/perl5220/bin/perl use strict; use warnings; print "Line1\n"; print "Line2\n"; =cut print "Line3\n"; =cut print "Line4\n"; =cut print "Line5\n"; =cut print "Line6\n";

    I added blank lines around all the =cut commands (which are now recognised as POD):

    $ podchecker podchecker_test_2.txt *** ERROR: Spurious =cut command at line 9 in file podchecker_test_2.t +xt *** ERROR: Spurious =cut command at line 13 in file podchecker_test_2. +txt *** ERROR: Spurious =cut command at line 17 in file podchecker_test_2. +txt *** ERROR: Spurious =cut command at line 21 in file podchecker_test_2. +txt podchecker_test_2.txt has 4 pod syntax errors.
    cat podchecker_test_2.txt #!/usr/bin/env perl use strict; use warnings; print "Line1\n"; print "Line2\n"; =cut print "Line3\n"; =cut print "Line4\n"; =cut print "Line5\n"; =cut print "Line6\n";

    I then applied my =for comment suggestion from earlier (syntactically correct and improved readability):

    $ podchecker podchecker_test_3.txt podchecker_test_3.txt pod syntax OK.
    cat podchecker_test_3.txt #!/usr/bin/env perl use strict; use warnings; print "Line1\n"; print "Line2\n"; =for comment print "Line3\n"; =cut print "Line4\n"; =for comment print "Line5\n"; =cut print "Line6\n";

    I believe the intention of the code examples in perlpodspec: =cut:

    =cut =cut The documentation ends here. ...

    is to show different, and entirely separate, examples of usage. It is not trying to suggest you should follow one =cut with another; indeed, the text immediately following the code indicates this would be syntactically incorrect and should cause a fatal error:

    "... error to try to start a Pod block with a "=cut" command ... Pod processor must halt parsing ... emit a warning."

    See also: perlpod, perlpodstyle, Test::Pod, and Test::Pod::Coverage.

    — Ken

Re: Is it safe/recommended to use pairs of =cut for multi line comments?
by Marshall (Canon) on Jun 10, 2016 at 17:51 UTC
    There are some other ways to "turn off" sections of code other than using the pod directives. For quickie debugging, I often use a simple: if(0){...} with the expectation that I will remove that once I find the program error. I guess a habit from writing other languages.

    For something that I expect to "live" as part of the code, I sometimes do this:

    use constant DEBUG => 0; if (DEBUG){ # block is optimized away at compile time .... # but I can turn in "on" again easily } # very readable and effective
    I felt pairs of "=cut" is easier to remember than "=pod" or "=begin/end comment"
    There is really only one thing to remember, "=cut", ends that section. You don't have to use =pod to start, use anything that you want. I think you have a confusing mess if you use =cut to start a pod block although that does work, despite the documentation to the contrary. For debugging, I guess anything goes, but I believe it unwise to go against the documentation because a someday Perl might actually wind up doing what the documentation says!

    Your question is more of "how do I disable code" rather than permanent multi-line textual comments which I think is a different subject.

Re: Is it safe/recommended to use pairs of =cut for multi line comments?
by Laurent_R (Canon) on Jun 10, 2016 at 17:50 UTC
    I would add that using the same =cut tag for the start and the end of the commented out sections isn't very clear as soon as you start to have more than one such section and can lead to errors.
Re: Is it safe/recommended to use pairs of =cut for multi line comments?
by ww (Archbishop) on Jun 10, 2016 at 21:33 UTC

    YAW (AKA 'TIMTOWTDI" and cousins)

    active code blah blah; =head This is a comment. Note the blank lines. =cut ...more code
    FWIW....
[Solved]: Is it safe/recommended to use pairs of =cut for multi line comments?
by Perl300 (Friar) on Jun 10, 2016 at 19:07 UTC
    Thank you all for your your replies. I will follow suggested approach in the documentation as I know pros and cons lot better now. I like to know why I am doing something and as always I got valuable inputs here.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1165314]
Approved by stevieb
Front-paged by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-24 22:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found