Re: Does "my" vs "our" explain this problem?



CedricCicada@xxxxxxxxx wrote:
Greetings!

I have a script that calls a module. In the script, I define a global
variable using "my" and assign it a value.

That is a contradiction in terms. 'my' variables, by definition, are
not global. They are lexically scoped to the innermost block in which
they were declared. If there is no block, they are file-scoped.

In the module, I use
$main::myGlobal to read the global variable, and I find that it is
empty. Should I define the global variable using "our" instead?

'our' is the only way to "declare" a global variable*. You did not
have a global variable. You had a lexical variable.

Google for "Coping with Scoping."

mymoduletest.pl:

use MyModule;
my $myGlobal = "This is a global variable.";

No, it's not. It's a lexical variable, who's scope happens to be the
entire scope of the file mymoduletest.pl. Anywhere outside that file,
that variable does not exist.

MyModule::myMethod();

MyModule.pm:

sub myMethod()
{
print "Global variable contains $main::myGlobal\n";

This is the global variable $myGlobal that belongs to the package main.
}

This is, in general, really bad form. Your packages should not have
any access to the package 'main' without very good reason (and I can't
think of any good reasons right now). While it makes more sense for
package main to have access to your module's variables, it's still bad
form - declare all variables lexically, and right accessor subroutines
and methods.

Paul Lalli

* 'our' doesn't actually "declare" global variables, because global
variables are not declared, they simply exist. 'our' simply allows you
to use that global variable without fully qualifying it (ie, preceding
it with its package's name) even though strict vars is in scope, for
the duration of the lexical scope. Anywhere outside the 'our''s scope,
you still have to fully qualify the variable if strict is in effect.

.



Relevant Pages

  • cron script hangs after a while (seems to)
    ... these scripts from my package, ... Create an html page from the log created by the previous script. ... declare ip_src_table ...
    (comp.unix.shell)
  • Re: Trouble with variable scoping
    ... Variables declared with Care limited to the scope in which they are declared and any inner scopes, and are known as "local" or "lexical" variables. ... Variables declared with Care tied to a package and are stored as part of the package, and are known as "package" variables. ... Cdoes not declare a variable unless you run without the 'strict' pragma. ... local $foo; ...
    (perl.beginners)
  • Was re: inverting List::Compare
    ... I didn't do that even in this short script). ... It is always better to limit the scope of variables. ... I've also read the advice always to declare ... script books and tutorials?). ...
    (perl.beginners)
  • Re: Newbie Question on Variables
    ... scope of a For Each loop will not be accessible to Tasks *outside* the loop. ... In the Script Task Editor, ... access to these variables anywhere in my package, ...
    (microsoft.public.sqlserver.dts)
  • Re: Trouble with variable scoping
    ... I don't understand is why, or what I should declare the variable as, ... Variables declared with Care limited to the scope in which they are ... the inner scope masks the outer, but the outer variable still exists, it ... Variables declared with Care tied to a package and are stored as ...
    (perl.beginners)