Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Block commenting

by sweetblood (Prior)
on Nov 04, 2003 at 16:11 UTC ( [id://304458]=perlquestion: print w/replies, xml ) Need Help??

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

I know that c-style multiline commenting is not available in Perl, and the camel says there is no multi-line commenting (other that placing a # in the front of each line), but I could swear I've seen something like it in some script somewere. I'm just looking to comment a block of code while I debug and test not to actually make code comments. Does anyone have a suggestion?

Replies are listed 'Best First'.
Re: Block commenting
by duct_tape (Hermit) on Nov 04, 2003 at 16:29 UTC

    You can use POD to comment out large blocks.

    =begin comment code to comment out goes here.... =cut
      Unless the blocks themselves contain POD. Or a here document or string with \n=cut\s in it.

      Abigail

      I believe this is what I've seen ... Thanks!
        I also use POD to comment out blocks of code. I also never leave those blocks in for long (or at least strive not to). One trick i learned from merlyn is to be descriptive with the POD and use something like:
        =for comment blah blah blah =cut
        But you are still going to interfere with any real POD your code contains - which is why i strive to remove unneeded code ASAP and let CVS handle the history (but i still leave phantom code from time out of false laziness).

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: Block commenting
by thraxil (Prior) on Nov 04, 2003 at 16:21 UTC

    either get a decent text editor that will let you comment an entire region at once, or do something quick and dirty like:

    if(0) { ... }
      Unfortunetly, we all don't get to work with just any text editor. I'm stuck with what they give me, but I do like your other suggestion. Thanks!

      Commenting out a region isn't the same as a multi-line comment, of course. Using if(0){ will work for (most) code but not for arbitrary text.

      -sauoq
      "My two cents aren't worth a dime.";
      
Re: Block commenting
by Abigail-II (Bishop) on Nov 04, 2003 at 16:23 UTC
    POD, here docs, strings, Acme::Comment, smart editors, editor macros, it all works. Take your pick.

    Abigail

      To complete that list, and for no other reason, I will mention #if 0/#endif with perl's -P switch.
Re: Block commenting
by Roger (Parson) on Nov 04, 2003 at 16:20 UTC
    Why not try to wrap your code inside a dummy heredoc assignment, say,

    my $dummy = <<'COMMENT' ... your code here ... ... ... COMMENT ;
    or just
    # was << COMMENT <<'COMMENT' ... your code block here ... ... ... COMMENT ;
    Which will comment out a block of code.

    Update: Thanks to liz for pointing out the problem with my initial un-single-quoted HEREDOC. I have improved the solution by adding a single quote around the COMMENT. However this solution is still flawed - see flounder99's post below.

      Not a very good idea, as the comment still is interpolated:
      use strict; <<COMMENT; $foo = 'bar'; COMMENT
      gives:
      Global symbol "$foo" requires explicit package name at line 3.

      Abigail-II's suggestion of Acme::Comment is pretty serious, if you really want to have multi-line comments the way you want them. Personally, I think Acme::Comment lives in the wrong namespace: it should probably live in the Filter::Comment namespace or so.

      Liz

        Personally, I think Acme::Comment lives in the wrong namespace: it should probably live in the Filter::Comment namespace or so.
        I disagree with that. I think that namespaces like Filter::* and Tie::* should be restricted to module that deal with filtering or tieing. It's just not right to make the implementation part of the name space. Just think about it, should all modules that use objects belong in the OO::* name space? And what are you going to do with a module that uses a filter, is hence named Filter::Whatever, and then modifies its implementation so that it uses a different technique?

        It's a bad, bad suggestion.

        Abigail

        use strict; use warnings; <<'COMMENT'; $foo = 'bar'; COMMENT
        Will avoid the interpolation. But you will still get
        Useless use of a constant in void context at temp.pl line 3.
        To avoid that you can do:
        use strict; use warnings; { no warnings; <<'COMMENT'; $foo = 'bar'; COMMENT }
        But that is a lot of work.

        --

        flounder

        Thanks++ for pointing out that the comment is interpolated. However to fix this, I would simply add single quotes around the COMMENT to prevent the HEREDOC to be interpolated.

        use strict; <<'COMMENT' $foo = 'bar'; COMMENT ;
Re: Block commenting
by matthewb (Curate) on Nov 04, 2003 at 16:23 UTC
    You haven't seen multiline commenting (in the sense that you describe) in a Perl program but it is feasible to me that you recall seeing some POD somewhere and remember it as a comment. Check perlpod for a refresher.

    I find it helpful to bind cperl-comment-region to a handy key in emacs - that way block-commenting is only ever a couple of keystrokes away.

    MB
Re: Block commenting
by hardburn (Abbot) on Nov 04, 2003 at 16:48 UTC

    I use the vim block comment script. I changed the commands to use the comma key instead of the period, or else it tends to sit there waiting for you to type something more when you want to repeat the last command.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

Re: Block commenting
by Anonymous Monk on Nov 05, 2003 at 07:58 UTC

Log In?
Username:
Password:

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

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

    No recent polls found