in reply to Strange compile error?

G'day gelbukh,

Welcome to the Monastery.

"Sorry for a stupid question! But I want to learn: ..."

A '#' introduces a comment. See "perlsyn: Comments". So, when compiling, Perl sees your lines 9 to 12 as:

my $N= say "N = $N";

As Perl doesn't care about the intervening whitespace, that's basically:

my $N= say "N = $N";

Assignment is a right-to-left operation. See "perlop - Perl operators and precedence". That's a fairly long page; the parts most pertinent to your question are:

So, Perl attempts to compile 'say "N = $N"' first. It hasn't evaluated the left side of the assignment ('my $N') yet, so it sees $N as a "Global symbol" and hence the error message.

There is a subtle point here that isn't immediately, or intuitively, obvious. A statement such as 'my $x = ...;' has two distinct parts:

I see a couple of posts showing how to get length and last index of an array. Another way to get the length is:

my $N= 0+@Org;

As a further tangential point, a "use VERSION" statement, where VERSION is 5.12 or greater, you get 'use strict;' automatically. I like to put the use VERSION statement first: if the user does not have a sufficiently high enough version, you might as well stop here rather than doing other (effectively) pointless processing. Accordingly, instead of

use strict; use warnings; use 5.020;

I'd write

use 5.020; use warnings;

Just something to consider for future reference.

— Ken

Replies are listed 'Best First'.
Re^2: Strange compile error?
by afoken (Chancellor) on Apr 30, 2024 at 13:46 UTC
    A '#' introduces a comment.

    Oh no! You triggered that annoying little bean counter! So here we go ...

    A '#' does not ALWAYS introduce a comment:

    • $#array is the last index of @array
    • # can be used in place of / in s///, m//, q//, qq//, qx//, qw//, y///, tr///.
    • $# was a magic variable, deprecated long ago, removed in Perl 5.10.0 (see perlvar)
    • # inside a comment is ignored like any other character until end of line
    • And of course, a # in a string constant is just that. It does not start a comment inside a string constant.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      BUT ...

      • Inside POD, you forgot POD.

      WAIT...

      • After __DATA__ and __END__

      STOP ...

      • could be inside a here-doc marker too
      MOMENT ...

      • A source filter could transform them (see half of f*ck!ng ACME:: ...)

      HOLD ...

      • what if C-preprocessor macros are activated?
      AND ...
      • (fill in more nitpicking, yawn 🥱)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

      UPDATES

      Inserted 3 more criteria in hindsight... 🙃

Re^2: Strange compile error?
by LanX (Saint) on Apr 29, 2024 at 19:47 UTC
    > Assignment is a right-to-left operation

    That's right, but sorry you are overcomplicating things.

    Precedence isn't relevant here:

    DB<1> use strict; my $N = $N; Global symbol "$N" requires explicit package

    Declarations are only effective after the statement, in this case behind the semicolon.

    This was done to allow accessing equally named variables from a surrounding scope.

    See perlsub#Private-Variables-via-my()

    The declared variable is not introduced (is not visible) until after the current statement. Thus,

     my $x = $x; 

    can be used to initialize the new $x with the value of the old $x, and the expression

     my $x = 123 and $x == 123 

    is false unless the old $x happened to have the value 123.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      I was talking about associativity (as in "right-to-left" operation). The table to which I referred lumps associativity and precedence together: there's not a lot I can do about that.

      — Ken

        But it doesn't matter which part of a statement is compiled first, be it by associativity or precedence or quantum flux.

        The docs are clear:

        The declared variable is not introduced (is not visible) until after the current statement.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery