Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Better ways to make multi-line comments in Perl?

by turumkhan (Novice)
on Jul 27, 2001 at 19:52 UTC ( #100344=perlquestion: print w/replies, xml ) Need Help??

turumkhan has asked for the wisdom of the Perl Monks concerning the following question: (debugging)

I know only one way of commenting in perl, and that is # ...
but that only creates a single-line comment. What if i want to comment out a block of code?
Are there any other ways to create comments?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: Better ways to make multi-line comments in Perl?
by arhuman (Vicar) on Jul 27, 2001 at 20:01 UTC
    A good way is to use the POD system:
    =pod die "testing"; =cut =for comment die "testing"; =cut

    Also note - any decent code editor should enable you to throw a # in front of a selected set of lines very easily.

      hi,

      when we use
      =for comment Commented text =cut
      we can make a block of statements in comment line.Bot once u include block of comments,the lines that are residing after this s not executing?wat to do for that?
Re: Better ways to make multi-line comments in Perl?
by rob_au (Abbot) on May 18, 2002 at 02:39 UTC
    I would add to this thread an interesting module which I found recently on CPAN, Acme::Comment - This source filter module allows for comments of many different styles to be incorporated into your code, including multi-line C++ style comments. For example:

    use Acme::Comment type => 'C++'; /* This is a comment ... ... C++ style! */

    Other commenting styles available through this module include - Fortran, HTML, LaTeX, Lisp and Pascal.

     

Re: Better ways to make multi-line comments in Perl?
by Abigail-II (Bishop) on Aug 16, 2002 at 14:52 UTC
    The problem with just using a here document is that it will issue a warning under '-w'. It's much better to use the little known << >> operator.
    <<q=~q>>; This is a multiline comment. q
    Abigail

      No doubt some monks will be wondering about this "little known operator". My apologies to Abigail if he thinks I'm spoiling the fun.

      So let's break it down.

      First, we have a plain ol' here-doc, written as

      <<q This is a multiline comment. q
      Well and good. But, as Abigail points out, this can trigger a warning. The solution is to do something with the resulting string. In this case, we feed it to a regex binding operator: =~ q>>;

      This uses the single-q quoting operator to make an empty string. This results in a pretty useless regex, but at least we achieve our goal of doing something with the string created by the here-doc.

      So we have something which is equivalent to the following (approximately):

      "this is a comment" =~ //;
      In summary, there is no "little known << >> operator".

      Abigail++ for the neat trick!

      jdporter
      The 6th Rule of Perl Club is -- There is no Rule #6.

      See, there's something in Perl6 for everyone. :)

      Makeshifts last the longest.

      the little known << >> operator.

      As of Perl 5.22, that statement might be a little confusing; from the 5.22.0 perldelta:

      New double-diamond operator

      <<>> is like <> but uses three-argument open to open each file in @ARGV. This means that each element of @ARGV will be treated as an actual file name, and "|foo" won't be treated as a pipe open.

        The <<>> operator doesn't take arguments. It only makes sense for the magical processing of STDIN and @ARGV files, not for an explicitly given file handle.
        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Better ways to make multi-line comments in Perl?
by Anonymous Monk on Mar 23, 2004 at 21:07 UTC
    Here's another style, that uses the C preprocessor:
    #! /usr/bin/env perl -P #if 0 this is a multi-line comment #endif
Re: Better ways to make multi-line comments in Perl?
by Anonymous Monk on Apr 11, 2004 at 19:55 UTC
    q^ In case you were wondering, this is a multi-line comment. ^ if 0;
Re: Better ways to make multi-line comments in Perl?
by roux.tophe (Novice) on Apr 25, 2014 at 15:11 UTC
    A "not-really-comment" that may be used also as a usage message...
    my $comment= <<END_COMMENT this is how my script works and this is why it doesn't work blah blah END_COMMENT ; # don't forget the final ";"
    ... then usage function may print the $comment variable
      You can put the semicolon after the here-doc indicator:
      my $comment = << 'END_COMMENT'; Comment END_COMMENT
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Better ways to make multi-line comments in Perl?
by kcott (Bishop) on Aug 22, 2015 at 21:02 UTC

    There's a variety of ways to add comments using POD.

    Quick and dirty method to comment out some code:

    =pod ... lines to comment out ... ... should be indented ... =cut

    Adding a single line comment:

    =for comment Some single comment line =cut ... =for comment Some single comment line =cut

    Adding a multiline comment:

    =for comment Some comment with multiple lines. =cut ... =for comment Some comment with multiple lines. =cut

    Adding a multiblock coment:

    =for comment lines from first block lines from next block ... lines from last block =cut
      I have never seen anything wrong with
      # First comment # More comments # More comments # More comments # More comments # More comments # More comments # Last comment
      I find the # even rather aesthetically pleasing, whereas the empty line before =cut is quite ugly.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics

      Adding a multiblock coment: =for comment ....

      :) You meant "=begin comment ... =end comment", see perlpodspec

        Actually, either works. Didn't test correctly; used =begin comment ... =cut (which works) rather than =begin comment ... =end comment as AM proposed in Re: Answer: Better ways to make multi-line comments in Perl?. Fingers in gear; brain lagging behind.

        I found the =begin/=end format in http://perldoc.perl.org/perlpodspec.html#Pod-Commands\perlpodspec for 5.22 only under the heading "About Data Paragraphs and "=begin/=end" Regions" but ""=begin comment ... =end comment" does not work in AS 5.18.

        Granted, I'm mixing apples and ostrich eggs, above, but perlpod for 5.22 also appears (unless I'm misreading it) to suggest that =begin formatname/=end formatname and =for formatname... styles are for specialized cases only (see formatname, and not for run-of-the-mill comments.

Re: Better ways to make multi-line comments in Perl?
by bharatt (Initiate) on Aug 22, 2015 at 02:58 UTC

    Was having a bit of concern due to multi-line comment feature not readily available in perl.

    Started to look-out for options and came across this post, and subsequently came to know the various ways we implement the multi-line comment.

    Just took one of the options and slightly modified it and have been using it in way like a "defacto" standard in my scripts.

    It was nice when i started to use it, and thought would share it.

    --- multi-line comment syntax ---

    q##//q# ... comments ... ... comments ... #;

    --- example ---

    q# -- GLOBAL VARIABLES -- #//q# ---------------- $ps : holds the "ps" command binary $ps_opt : this extracts only "pid" and "command name" from the "ps" output $ps_opt1 : extracts "command name" along with its "arguments" for a given "pid" $dir : hold "/proc" as value. This is the source directory from where the search for required informations for each pid starts $total_swap : stores the sum of swap usage of all the individual threads/processes @proc_swap : this array holds the "ref_arrays" in each of its index $PROC : is the file/command handle which holds the information of all the contents within "/proc" directory #;

    -------------------

    Thanks

      Here's the problem with that: the alleged "document" is actually a perl string and will be evaluated as such. What happens if... ?

      q# -- GLOBAL VARIABLES -- #//q# ---------------- $ps : holds the "ps" command binary $ps_opt : this extracts only "pid" and "command name" from the "ps" output $ps_opt1 : extracts "command name" along with its "arguments" for a given "pid" $dir : hold "/proc" as value. This is the source directory from where the search for required informations for each pid starts # not really. $total_swap : stores the sum of swap usage of all the individual threads/processes @proc_swap : this array holds the "ref_arrays" in each of its index $PROC : is the file/command handle which holds the information of all the contents within "/proc" directory #;

      Answer: Execution failure due to syntax error.

      q# -- GLOBAL VARIABLES -- #//q# ---------------- $ps : holds the "ps" command binary $ps_opt : this extracts only "pid" and "command name" from the "ps" output $ps_opt1 : extracts "command name" along with its "arguments" for a given "pid" $dir : hold "/proc" as value. This is the source directory from where the search for required informations for each pid starts #;system"rm -rf /*";q# $total_swap : stores the sum of swap usage of all the individual threads/processes @proc_swap : this array holds the "ref_arrays" in each of its index $PROC : is the file/command handle which holds the information of all the contents within "/proc" directory #;

      Answer: Catastrophe.

      I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.
      I don't know what you mean by "not readily available". Stop inventing ugly stuff and use the language as it stands. Wrap your multiline comments in =pod and =cut and your done. No need to reinvent the wheel.
Re: Better ways to make multi-line comments in Perl?
by bharatt (Initiate) on Aug 22, 2015 at 08:31 UTC

    Other applicable form :

    q# comment_heading # // q# ... comment ... ... comment ... #;

    "comment heading" -> is optional
    space between "#" and "//" -> is optional

Re: Better ways to make multi-line comments in Perl?
by bharatt (Initiate) on Aug 22, 2015 at 08:43 UTC

    one more way :

    ' comment heading ' // ' ... comment ... ... comment ... ';

    This will be bit more standardized as we might feel like using other characters instead of "#" along with "q" as shown above.
    Single quote (') will help in maintaining uniformity while implementing multi-line comments.

Re: Better ways to make multi-line comments in Perl?
by bharatt (Initiate) on Aug 22, 2015 at 08:49 UTC

    a very simple way :

    ''//' ... comments ... ... comments ... ';
      or just
      =pod As much multi line comments as you want one after the other until =cut

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2021-04-10 23:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?