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

I was just thumbing through the Llama book and it says in the introduction, something like "comments are anything starting with an unquoted # character".

It occurred to me that that's not quite correct, because there are cases where you have #'s in the code, like:

@foo = (some array); $lastindexoffoo = $#foo;
so there's an unquoted # but it's not a comment.

What's the detailed version of the rule?
--

“Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
M-J D

Replies are listed 'Best First'.
Re: What's the exact rule about hash/pound?
by chromatic (Archbishop) on Feb 27, 2003 at 02:40 UTC

    Comments are anything starting with an unquoted #, or a # character that's not part of a term.

    %# = ( foo => 'bar', baz => 'buz' ); print keys( %# ), "\n";

    Unfortunately, you can't access an element of that hash directly because it looks like you want the last element of an array. Bug? Maybe, but I sure don't want to fix it.

    Update: Oh yeah, but yuck.

      IlyaM has said how to do it right, so I figured I will say how to do it wrong. :)

      This is bad, ugly, and deprecated*, but it works:   %#->{foo} If you want to stay legal you can do it semi-direct:   (\%#)->{foo} While on the theme of references:

      no strict 'refs'; '#'->{foo}
      which will work since %# can't be private.

      But don't do any of the above.

      ihb

      * It's labeled as a "known problem" in perl561delta, but in perl580delta it's called deprecated.

Re: What's the exact rule about hash/pound?
by Abigail-II (Bishop) on Feb 27, 2003 at 11:06 UTC
    You really don't want to know. Here's something to ponder about:
    q#This is a string#; q #This is a comment#;

    Abigail

Re: What's the exact rule about hash/pound?
by boo_radley (Parson) on Feb 27, 2003 at 01:49 UTC
    does $#foo start with an unquoted # character?
    I imagine the $ "binds" to the # more tightly than the # wants to be a comment.
      >does $#foo start with an unquoted # character?

      Er, well, no, but if the rule is "any time an unquoted # appears in a line of Perl, then the remainder of the line is ignored" then the rule is not correct in respect of that line, is it?

      Obviously there are special cases where that rule is qualified with an "unless blah blah blah" clause.
      --

      “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
      M-J D
        That's not the rule. Of course, the best way to know the rule is to read the source, but the summary of the rule you quoted originally is adequate to explain the behavior you describe. (and your later summary is very different in practice). Here's another rephrasing: If you're not inside quotes of some kind, a hash symbol that isn't inside another token starts a comment.
Re: What's the exact rule about hash/pound?
by paulbort (Hermit) on Feb 27, 2003 at 16:11 UTC
    OK, so the rule appears to be: a hash indicates a comment when it appears where Perl is expecting a new token.

    So "#" is not a comment because the quoted string is a token.
    Ditto $#foo.
    Did I miss anything?
    --
    Spring: Forces, Coiled Again!
      I feel I need to justify myself a little by quoting the part of Learning Perl which inspired this thread.
      Perl comments are like (modern) shell comments. Anything from an unquoted pound sign (#) to the end of the line is a comment. There are no C-like multiline comments.
      I mean, no disrespect to merlyn, it's the truth, it's nothing but the truth, but it's not the whole truth.
      --
      “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
      M-J D
Re: What's the exact rule about hash/pound?
by Mr. Muskrat (Canon) on Feb 27, 2003 at 13:53 UTC

    An unquoted # that follows whitespace or a semicolon is a comment.

    # this is a comment # as is this print "# I'm not!";# but I am

    Update: Ignore me! For I'm an idiot. ;-)

      Not true:
      print " # Not a comment";

      The reverse isn't true either:

      print "foo"#This is a comment

      Abigail

Re: What's the exact rule about hash/pound?
by hawtin (Prior) on Feb 27, 2003 at 10:57 UTC

    The rule is that a hash character looks like #

    But a pound character looks like £

    :-)

Re: What's the exact rule about hash/pound?
by hgolan30 (Initiate) on Mar 01, 2003 at 10:38 UTC
    its just another way to do do scalar(@foo) - 1 u can do for ($i=1;$i<$#foo;$i++) .....
Re: What's the exact rule about hash/pound?
by zouhair (Initiate) on Feb 28, 2003 at 15:57 UTC
    And what about the shabang : #!/bin/some_bin :) I love Perl because it's so permissive : NO RULEZ PLZ ! hm