Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

What is the easy way to comment out chunk of Perl Code

by gasho (Beadle)
on Nov 18, 2005 at 19:15 UTC ( [id://509909]=perlquestion: print w/replies, xml ) Need Help??

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

I would like to comment out chunk of perl code like in C++ /*commneted out*/ How can I do that in Perl, Thanks
  • Comment on What is the easy way to comment out chunk of Perl Code

Replies are listed 'Best First'.
Re: What is the easy way to comment out chunk of Perl Code
by philcrow (Priest) on Nov 18, 2005 at 19:19 UTC
    Well, usually we just run pounds down the leftmost column so other know for sure the code is out of use. This is facilitated in many editors (both inserting and removing). But if you insist on a short cut that isn't visually distinct through the block try this:
    =begin GHOSTCODE ... =end GHOSTCODE =cut
    This makes the code invisible to the compiler and to perldoc.

    Phil

      Phil ! Thank you very much Gasho
Re: What is the easy way to comment out chunk of Perl Code
by Roy Johnson (Monsignor) on Nov 18, 2005 at 19:22 UTC
Re: What is the easy way to comment out chunk of Perl Code
by kutsu (Priest) on Nov 18, 2005 at 19:24 UTC

    You can do it with POD (Plain Old Documentation). Take a look at POD in 5 minutes or perldoc perlpod.

    "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

Re: What is the easy way to comment out chunk of Perl Code
by jonadab (Parson) on Nov 19, 2005 at 02:42 UTC
    You can also use a single-quoted heredoc for this...
    <<'WONKYCODE'; print "This code isn't technically "commented out" per se; rather, it is interpreted as a string. However, it won't execute, and because of the single quotes it also won't interpolate. Assuming that the code you treat this way is an entire expression (not part of one), the string will be in void context and so will have no effect, other than to remind you that it's still here with a 'useless use of constant in void context' warning.\n"; WONKYCODE
Re: What is the easy way to comment out chunk of Perl Code
by thor (Priest) on Nov 18, 2005 at 22:48 UTC
    I typically use POD like this:
    =for comment #your commented code goes here =cut
    A little explanation. The "=for x" directive says that only a processor that's formatting for the x output type should heed what's in the following block. For instance, you could say "=for html" and then put some html markup in the block and it should be rendered only by programs seeking to render html. The nice thing about "=for comment" is that no one will be outputting a "comment" format...:-).

    thor

    Feel the white light, the light within
    Be your own disciple, fan the sparks of will
    For all of us waiting, your kingdom will come

Re: What is the easy way to comment out chunk of Perl Code
by zentara (Cardinal) on Nov 18, 2005 at 20:08 UTC
    For a quick block disabling, I usually just use
    =head1 stuff to be commented out =cut
    but it probably raises havoc with perldoc, so I manually insert # in code that I post.

    I'm not really a human, but I play one on earth. flash japh
Re: What is the easy way to comment out chunk of Perl Code
by ickyb0d (Monk) on Nov 18, 2005 at 19:22 UTC

    I'm pretty sure there isn't anyway. BUT! If you use vim, there's always vimscripts!

    For the vimscript below to work just put it in your .vimrc file. It assumes you have marked a maker 'a' (':mark a' while NOT in insert mode) at the beginning of where you want your un/comment block. Then just scroll down to where you want it to end and type in ',cb#' (while NOT in insert mode) to comment out the block. Alternatively you can type in ',ucb#' to uncomment a block of code.

    nmap ,cb# :'a,.s/^/#/gi<CR>:nohl<CR> nmap ,ucb# :'a,.s/^#//g<CR>:nohl<CR>

    Since perl doesn't have a standard IDE, i think the answer to this question is largely going to depend on what editor your using for developement.

      I use the BlockComment plugin for vim which I mapped to _c and _C to comment an uncomment respectively.

      It works really well with the highlighting mode that vim uses. For example, go to the start of a block (function, loop, if statement) then hit Shift-V % to highlight the entire block and then _c to comment it out. Or hit Shift-V 20j to highlight the next 20 lines below the cursor, then hit _c to comment or _C to uncomment.

      Oh, and it works for many different languages, not just perl.

      I couldn't get that to work with nmap ,cb#

      But it worked great with map <F8>. Thanks.

      Phil

Re: What is the easy way to comment out chunk of Perl Code
by Anonymous Monk on Nov 18, 2005 at 23:14 UTC
    if(0) { ...chunk of Perl code you don't want to run... }
      The problem with that is that perl is still going to try to compile what's inside the block, so it wouldn't work if you want to outcomment something that doesn't compile, or to outcomment an BEGIN/CHECK/INIT/END block - and it may not work as expected if you outcomment a method that also appears higher up in your inheritance tree.
      Perl --((8:>*
Re: What is the easy way to comment out chunk of Perl Code
by holli (Abbot) on Nov 20, 2005 at 09:23 UTC
    Every decent Editor supports Regular Expression Search & Replace. So you can simply mark all lines you want to comment out and replace "^" by "#".
    To remove the comments replace "^#" by "".
    That way one can also uncomment parts of the outcommented code again without ending up in a mess of "pseudo comment blocks".


    holli, /regexed monk/

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-12-08 02:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (50 votes). Check out past polls.