in reply to Newbie Q re: Undefined Value and "only used once" warnings
About the third error. Unike some cases with the "only used once" warning, Perl is right here.
You've messed up scoping rules, the $MAIN::Log you're trying to access is not the same as the $Log variable above.
$MAIN::Log is the $Log variable of the MAIN package, which can not be your Log for two reasons. Firstly, you declare that $Log with my, so it's a lexical variable, which does not belong to any package, instead, it belongs to the innermost lexical scope in which the declaration is, which is in this case the brace-block labeled MAIN. If you declare a my variable in that block, you can use its name only in that block. Secondly, there's no MAIN package here. The loop label MAIN does not create a package, you have to use the package statement for that. (You might be thinking that you are in package MAIN by default, in the absencse of pkg declarations. Not, that's package main in lc.)
There are two ways you could correct this. The first is that you use a lexical variable. You say my $Log before the opening of the MAIN loop, that way it is valid in the whole script (which is by itself a scope, although the largest one), and then you can refer it with $Log, that is, without a package name later.
The second is that you really use a global variable. In this case, you first have to say package MAIN somewhere, or just write main instead, which is the default package. Then, you change the declaration to our $Log= ... at the assignment, and wow, it's a global variable you can access as $MAIN::log or $main::log depending in what package you've just set.
The first solution is probably appropriate here, I've just written the second so that you know what $MAIN::Log really means here.
(Someone knowing pm please recommend some good tutorial about my and our in this thread. Thx.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Newbie Q re: Undefined Value and "only used once" warnings
by Nkuvu (Priest) on May 15, 2004 at 19:17 UTC |