I've just stumbled into a curious subtlety:
#!/usr/bin/perl -w
use strict;
PutFoo();
my ($foo) = "Goo";
PutFoo();
sub PutFoo
{
print "Boo hoo, foo is $foo\n";
}
It would seem that any code inside of PutFoo() would have
access to variables such as "$foo", but that does not seem
to be the case. Perl skips over the definition of $foo,
and runs PutFoo the first time without benefit of the
initialization.
"-w" puts out a curious warning, "Use of uninitialized value in concatenation (.) at
prog line 11." which doesn't seem to communicate much unless
you knew to expect this sort of thing.
What strikes me as odd about this is that Perl splits the
declaration and initial definition into two pieces. The
symbol is declared, as undef, but the definition is run
like a separate piece of code. This is quite unlike the
behavior of C++, for example, where the declaration and
assignment of initial value are one and the same, and
one cannot occur without the other if they are specified
together. Perhaps this is the price of a more dynamic language.
Is there a reason this is done, or is this a point of debate?
Thanks for any tips.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.