We don't bite newbies here... much | |
PerlMonks |
Curious about a quirk with the special variable, $0by mrdurtal (Scribe) |
on Jan 31, 2012 at 10:39 UTC ( [id://950943]=perlquestion: print w/replies, xml ) | Need Help?? |
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):
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:
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.
:There is more than one way to @verb a cat:
Back to
Seekers of Perl Wisdom
|
|