in reply to Curious about a quirk with the special variable, $0
I will admit from the start ... interested to find the filename printed instead of the expected "0".
Dang, dat don soun like no good english :D (I actually don't know, just sounds, well, interesting to me :)
FWIW, its all good, at least you're trying , to learn (as opposed just finish some program) :)
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);
That is the purpose, the hold the name of the script (hey, people want to know what it is, if they can )
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,
What undeclared variable, $0? Perl declares/defines/initializes $0 for you, its a pre-defined global variable
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?
This is equivalent to that for loop
#!/usr/bin/perl -- use strict; use warnings; { my $count = $0; LOOOOOP: while(1){ print "$count\n"; if( not ( $count < 10 ) ){ last LOOOOOP ; } else { $count++; } } undef $count; } __END__ $ perl junk junk Argument "junk" isn't numeric in numeric lt (<) at junk line 7. 1 2 3 4 5 6 7 8 9 10
Do you get it now?
Try another
#!/usr/bin/perl -- use strict; use warnings; { my $count = $0; LOOOOOP: { print "$count\n"; if( not ( $count < 10 ) ){ goto NO_MORE_LOOP; } else { $count++; goto LOOOOOP; } } NO_MORE_LOOP: undef $count; }
I apologise in advance for the "baby Perl" questions.
This free book covers this level of experience :) http://learn.perl.org/books/beginning-perl/
|
|---|