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.

:There is more than one way to @verb a cat:

In reply to Curious about a quirk with the special variable, $0 by mrdurtal

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.