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 | |
by LanX (Saint) on Apr 30, 2024 at 22:43 UTC | |
Re^2: Strange compile error?
by LanX (Saint) on Apr 29, 2024 at 19:47 UTC | |
by kcott (Archbishop) on Apr 29, 2024 at 23:08 UTC | |
by LanX (Saint) on Apr 30, 2024 at 00:19 UTC |