I understand what Perl is doing, but it is a bit misleading
to have the variable pseudo-defined, but not fully defined.
For instance, Perl knows that there is a variable called
$foo in the first call, the parser has picked it out, but
it does not know what it is. If you change the reference in
PutFoo() from "$foo" to "$bar", all sorts of warnings go off
and the program will not run. "$bar" is a deal-breaker for
compilation.
The only reason I've even discovered this is because I wanted
to have some "static" data for a function, but wanted to
minimize the "commute" between editing the function and
editing its associated data. What was irritating was that
Perl
insisted that my variable was "defined", but
it did not contain any data. There is no warning for hashes
or arrays unless you are printing them. 'strict', '-w',
and 'taint' were all cool with my wacky, disordered
definition. I wasn't.
#!/usr/bin/perl -wT
my ($global_vars....) = (global_var_definitions);
# : Continues for some time
sub func1 {}
sub func2 { calls FuncN(), for example }
# : many functions
my (%funcN_data) = ( stuff );
sub funcN { reference to %funcN_data fails silently }
The only solution is to carefully organize the order of the
functions, or, uh, use the
BEGIN{} solution proposed by
tye, which I will have to agree, is a little out of the
ordinary. I mean, it gets the job done, but at what price?
I can only assume that the reason Perl behaves this way
is that it is too difficult to implement differently, or because
of consensus or preference on the part of the implementor,
perhaps even because of the difficulty in implementing it.
Sure would be nice to have a real error, though.