mrdurtal has asked for the wisdom of the Perl Monks concerning the following question:
I will admit from the start that these may be some of those "dumb a**" questions that some people scowl at, but I'm curious. I was setting up a little demonstration program and I mistyped the following code and got what was a curious output at the time.
First the code (on a HP Netbook running Trisquel/Slaine, Libre-Linux, with Perl 5.10):
#!/usr/bin/perl #forcount use strict; use warnings; for (my $count = $0; $count < 10; $count++) { print "$count\n"; }
I had not noticed that I had initialised the count with a "$0" instead of "0". I had run "perl -c forcount" and syntax was ok (I regularly forget semicolons). Then, I ran the program and got the following output:
Argument "forcount" isn't numeric in numeric lt (<) at forcount line 5 +. forcount 1 2 3 4 5 6 7 8 9
I realised that I had fouled up the initialisation, but was interested to find the filename printed instead of the expected "0".
So, after a bit of investigation, here are my remaining questions:
a) What is the actual purpose of the special scalar variable, $0, which holds the filename of the script (I have read perlvar, but still don't understand the significance);
b) Did I not get a warning about the undeclared variable because it was being assigned to a declared variable, or did I not get it because it was a special variable such as $_ and why should this be the case; and,
c) Did the count iterate to 1..9 because the special scalar variable, $0, was treated as zero due to the numeric lt (<) operator? Or, in other words, how was the condition able to be evaluated if it did not conform to initial expectations by being non-numeric?
I apologise in advance for the "baby Perl" questions.
|
---|