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.
| [reply] [d/l] |
|
=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?
| [reply] [d/l] |
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. | [reply] [d/l] |
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 | [reply] [d/l] [select] |
|
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.
| [reply] [d/l] [select] |
|
| [reply] |
|
| [reply] [d/l] [select] |
|
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.
| [reply] [d/l] |
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
| [reply] [d/l] |
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;
| [reply] [d/l] |
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
| [reply] [d/l] |
|
You can put the semicolon after the here-doc indicator:
my $comment = << 'END_COMMENT';
Comment
END_COMMENT
| [reply] [d/l] |
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
| [reply] [d/l] [select] |
|
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
| [reply] [d/l] [select] |
|
Adding a multiblock coment: =for comment .... :) You meant "=begin comment ... =end comment", see perlpodspec
| [reply] |
|
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.
| [reply] [d/l] [select] |
|
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 | [reply] [d/l] [select] |
|
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.
| [reply] [d/l] [select] |
|
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.
| [reply] |
Re: Better ways to make multi-line comments in Perl?
by bharatt (Initiate) on Aug 22, 2015 at 08:31 UTC
|
q# comment_heading # //
q#
... comment ...
... comment ...
#;
"comment heading" -> is optional
space between "#" and "//" -> is optional
| [reply] [d/l] |
Re: Better ways to make multi-line comments in Perl?
by bharatt (Initiate) on Aug 22, 2015 at 08:43 UTC
|
' 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.
| [reply] [d/l] |
Re: Better ways to make multi-line comments in Perl?
by bharatt (Initiate) on Aug 22, 2015 at 08:49 UTC
|
''//'
... comments ...
... comments ...
';
| [reply] [d/l] |
|
=pod
As much multi line comments as you want
one after the other until
=cut
| [reply] [d/l] |