18

How do I comment a part of a single line in Perl, like the following line:

 if($clevel==0){#never happends}

I would like to be able to comment that last closing bracket, without going to a new line.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
john-jones
  • 7,490
  • 18
  • 53
  • 86
  • Please be specific: copy-and-paste into your question the line of code you're working with, and tell us which part you'd like to selectively comment. – Greg Bacon May 12 '10 at 13:52
  • What's wrong with what you have? Are you closing the block with other code? If you aren't going to run the block, why don't you comment out the if too? – brian d foy May 15 '10 at 23:29
  • 1
    Off course I'm asking this mostly to know how to do this in general. This one instance by itself is not a major thing. But the reason is increased readability. If I comment out the line it changes color and it also becomes misaligned with the other if($clevel==x) lines. – john-jones May 16 '10 at 09:56

6 Answers6

12

If it's really that important, use source filtering.

# C_Style_Comments.pm
package C_Style_Comments;
use Filter::Simple;
FILTER {   s{/\* .* \*/}{}gmx    };
1;

$ perl -MC_Style_Comments -e 'print /* 5, No wait, I mean */ 3'
3
mob
  • 117,087
  • 18
  • 149
  • 283
  • +1 for source filters. The way you have it, it looks like you could even comment out parts of a comment or a string. Cool – Axeman May 12 '10 at 15:00
  • 11
    Source filters are evil. Suddenly you can't trust the code in front of your eyes, because some invisible magic elsewhere in the program might be altering it. It may be useful in development, but stay far far away from it in production code. IMHO. – Ether May 12 '10 at 16:11
  • 2
    Like many evil things, there's a time and a place for source filters (this question isn't necessarily one of those times). Always wear your safety goggles. – mob May 12 '10 at 16:37
  • 3
    @Ether - I uploaded `Acme::SafetyGoggles` to CPAN for you ;-) – mob May 12 '10 at 17:34
  • 1
    rule: heh, that's an awesome module! But people who use Acme modules are assumed to know what they're getting themselves into... I was just targeting that advice to beginners. :) – Ether May 12 '10 at 18:04
11

The # sign starts a comment that ends with the end of the line.

lexu
  • 8,766
  • 5
  • 45
  • 63
7

Any reason you can't write :

if($clevel==0){#never happends}

as :

if($clevel==0){} #never happens

There are some tricks you can do to hide messages, such as:

0 and 'some comment'

But you're just going to make it more confusing if someone else has to maintain your code in the future.

Working within the constraints of a language, rather than trying to force it to act like some language you're more familiar with often leads you to learn new things. I personally hate working in IDL, but some of the tricks for dealing with poor loop performance led me to optimize code I've since written in other languages.

Joe
  • 2,547
  • 1
  • 18
  • 27
  • 2
    For complete code maintainance, you shouldn't just comment on an "impossible condition", you should warn or die or print some amusing message (because that gets around) so that you *know* when the code has hit one of those "impossible" lines. – Axeman May 12 '10 at 22:22
  • @Axeman: I prefer to avoid 'die' after debugging some code where someone didn't consider how things might change in the future, thus hitting one of those 'this should never happen' sections. But you're right -- `warn 'this should never happen'` would accomplish the specific case that was asked. – Joe May 12 '10 at 23:42
  • Ah, this is something one of my professors once recommended as a way to "comment out" sections of code when they don't have a multiline comment mechanism – Honinbo Shusaku Dec 12 '16 at 16:33
  • @Abdul : multiline comments are tricky, because it can be difficult to tell when the comment ends if it's very, very long. Or if you try commenting out a block which contains another comment. But you can do it in perl with '< – Joe Dec 12 '16 at 18:25
  • And then years later, after leaving a job, I get an email asking why my code produced the message `this should never happen`. Sure enough, someone changed some logic so it entered the 'never happens' block. – Joe Apr 24 '19 at 17:57
6

A # and then a line break. You can treat them as a bracket of sorts, since little in Perl looses its meaning from being on different lines.

my $ans = 2 + rand( 5 ) + $pixels / FUDGE_FACTOR;

To

my $ans = # 2 + 
    rand( 5 ) + $pixels # / FUDGE_FACTOR
    ;

Or from:

if ( dont_know_how_this_breaks() && defined $attribute ) { 
   #...
}

To:

if ( # dont_know_how_this_breaks() && 
     defined $attribute ) { 
   #...
}
Axeman
  • 29,660
  • 2
  • 47
  • 102
  • Yeah that's true, but i ment to write something, then comment and then write again. All within the same line. – john-jones May 12 '10 at 12:58
  • @Hermann Why doesn't splitting the line of code as in this answer do what you want? – Greg Bacon May 12 '10 at 13:53
  • because it requires me to split the line. i want the sentence to continue right after the comment. a small explanation for a single word in a sentence shouldn't require to break the line. the code im writing is the following: if($clevel==0) {#never happends} here it would be nice if i could finish that one closing bracket without going to the next line. its kinda like not needing to make a newline anytime one puts an [] into a quotation. requiring that newline operator can decrease readability and compactness. And at last, how do i force a newline into a comment here? – john-jones May 12 '10 at 14:42
  • 2
    @Hermann Ingjaldsson: Just commenting that something doesn't happen isn't as useful as doing something like *asserting* it never happens. I've had code hit never-happens lines, and it's useful to report that it's hit a condition you never thought possible. – Axeman May 12 '10 at 22:18
4

Use a string as an inline comment:

perl -lne '$a++; q#some explanation#;print;'

Inline commenting is necessary for commenting perl code embedded in Makefiles (and Bash script and Vim Scripts) as in this vimscript call perl .. VIM thesaurus file

mosh
  • 1,402
  • 15
  • 16
-1

Part of line or multiply line comment in perl:

=comment
...
...
...

=cut
Michael Horojanski
  • 4,543
  • 4
  • 33
  • 34
  • 2
    I've seen this syntax for multi-line comment, but not for partial line comment. I'm not 100% sure, but I think you're wrong in your claim that this syntax can be used for a partial line comment. – oyvey Nov 23 '16 at 11:54