in reply to Scoping Study Aid program

Scoping does make my brain hurt: I get:
Name "Our_package::my_variable" used only once: possible typo at scope.pl line 41.
for
print "4) \$Our_package::my_variable NOT DEFINED\n" if not (defined $Our_package::my_variable);
Shouldn't $Our_package::my_variable be something? I'm looking for a typo but ...

a

Replies are listed 'Best First'.
Re: Re: Scoping Study Aid program
by zeno (Friar) on Jan 16, 2001 at 14:14 UTC

    I get:
    Name "Our_package::my_variable" used only once: possible
    typo at scope.pl line 41.
    for

    print "4) \$Our_package::my_variable NOT DEFINED\n" if not (defined $Our_package::my_variable);
    Shouldn't $Our_package::my_variable be something?
    I'm looking for a typo but ...

    You're right! I get the warning, too, but I let this happen on purpose. This generates a warning because I purposely refer to a variable declared 'my my_variable' in the other package. Since it's declared with 'my', it is (let's see if I get this right) lexically scoped only to the other package, so the calling program can't see it. As I understand it, a variable with lexical scoping means that it only exists within a particular textual area of code, in this case, the package "Our_package". I guess a solution to this would be for me to change the "#!/usr/bin/perl -w" to "#!/usr/bin/perl". Or maybe print a disclaimer about the warning. The point is, the user should learn from this that a 'my' variable in a package is not visible from outside that package.
Re: Re: Scoping Study Aid program
by zeno (Friar) on Jan 16, 2001 at 14:18 UTC
    I went ahead and changed the code and included a disclaimer. Enjoy! :) -tim