Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Commenting out script in Perl like in C++

by ozgurp (Beadle)
on Jul 26, 2003 at 09:59 UTC ( #278075=perlquestion: print w/replies, xml ) Need Help??

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

Is it possible to comment out part of the code in Perl like in C++?
/* This part contains the code being commented out. */

Replies are listed 'Best First'.
Re: Commenting out script in Perl like in C++
by moxliukas (Curate) on Jul 26, 2003 at 10:15 UTC

    There are probably three approaches to multiline comments that I can think of at the moment.

    • using POD

      This one is part of the Perl language itself. it is fully documented in perlpod manual. You would do it like this:

      .... some code .... =pod multiline comment (or just code that you don't want to be executed) =cut ...some more code...
    • Working around with if (0) {...}

      This is juat a workaround, where you place the code in a if block that is never executed (you can't put just any text there, because it is Perl code after all). However, you specifically asked for commenting out of Perl code, so this might be applicable to you

      ...some code... if (0) { ...some code commented out... } ...some more code...
    • use ACME::Comment

      There is a module ACME::Comment that let's you use any kind of commenting style you want. This example is taken straight from it's documentation:

      use Acme::Comment type=>'C++', own_line=>1; /* if (ref $mod) { $bar->{do}->blat(msg => 'blarg'); eval { i'm sooo sick of this time for some coffee */ // I prefer beer. --sqrn

    Hope this helps.

      If you use if (0), there are a few things you should be aware of. First, the code is still complied - so your outcommented code should be valid, otherwise your program won't compile. Second, things that have a compile time effect still happen. This includes, but is not limit to, use, BEGIN, our and subroutine declaration.

      Abigail

Re: Commenting out script in Perl like in C++
by Abigail-II (Bishop) on Jul 26, 2003 at 10:06 UTC
    Not like in C or C++, Perl doesn't have multiline comments. However, Perl does have POD, and you could do something like:
    =begin comment Code to be outcommented. =end comment

    Just make sure that the code you are outcommenting doesn't have a =cut line.

    Of course, with any decent editor, putting # at the beginning of each line of a selected block (or removing them) is a piece of cake.

    And there's Acme::Comment, which would allow you to use C style comments, but that uses a simple source filter and it could filter out code that only looks like a comment, but isn't.

    Abigail

Re: Commenting out script in Perl like in C++
by PodMaster (Abbot) on Jul 26, 2003 at 10:43 UTC
    `perldoc comment' yields
    Found in C:\Perl\lib\pod\perlfaq7.pod
      How can I comment out a large block of perl code?
                Use embedded POD to discard it:
    
                    # program is here
    
                    =for nobody
                    This paragraph is commented out
    
                    # program continues
    
                    =begin comment text
    
                    all of this stuff
    
                    here will be ignored
                    by everyone
    
                    =end comment text
    
                    =cut
    
                This can't go just anywhere. You have to put a pod directive
                where the parser is expecting a new statement, not just in the
                middle of an expression or some other arbitrary yacc grammar
                production.
    
    comments yields C style comments?.
    perl comments yields Better ways to make multi-line comments in Perl?.

    You might wanna also read the PerlMonks FAQ and this tutorial -- ain't perlmonks great ;)

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Commenting out script in Perl like in C++
by perrin (Chancellor) on Jul 26, 2003 at 16:26 UTC
    The emacs cperl-mode makes it easy to comment/uncomment large blocks at once.
Re: Commenting out script in Perl like in C++
by Aristotle (Chancellor) on Jul 26, 2003 at 19:49 UTC
    Visual block mode in vim makes it a breeze to add/remove comment markers to long stretches of code.

    Makeshifts last the longest.

Re: Commenting out script in Perl like in C++
by Theo (Priest) on Jul 27, 2003 at 04:58 UTC
    Putting things between paired  =cutcommands works well also.

    ... operating code ... =cut code or comments that are ignored =cut ... more functional code
    -ted-

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2022-05-24 03:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (82 votes). Check out past polls.

    Notices?