didess has asked for the wisdom of the Perl Monks concerning the following question:

Hi all ! I'm trying (unsuccessfully) to use "our" variables outside of their defining package without qualifying them by the package name . Example follows. I tried with Perl 5.8.8 on Windows (XP) and AIX (5.3) with the same result. The package (file tryOur.pm):
#!/usr/bin/perl # ----------------- package tryOur; our $ExecDir = "/usr/local/bin"; # 1;
The "caller" :
#!/usr/bin/perl # ---------------- use tryOur; printf ("U line %d: ExecDir=%s\n",__LINE__,$ExecDir); printf ("U line %d: ExecDir=%s\n", __LINE__,$tryOur::ExecDir);
The result on the screen:
U line 6: ExecDir= U line 7: ExecDir=/usr/local/bin
Why don't i get the value of $ExecDir when not prefixing it with the package name ? I'm sure to do something wrong, but what ? Thanks in advance. Didier

Replies are listed 'Best First'.
Re: I can't see "our" variables from outside the package
by ikegami (Patriarch) on Aug 23, 2008 at 11:33 UTC

    Three problems.

    1. Your node is unreadable. Put your code inside of <c>...</c>.

    2. our is lexically scoped.

    3. You're not even in the same package anymore, How do you expect Perl to know which package contains the variable if you don't use its full name?

Re: I can't see "our" variables from outside the package
by moritz (Cardinal) on Aug 23, 2008 at 07:38 UTC
    Why don't i get the value of $ExecDir when not prefixing it with the package name ?

    That's the whole point of packages: they hide stuff, unless you explicitly "unhide" it by either importing it, or by using the full qualified name.

Re: I can't see "our" variables from outside the package
by GrandFather (Saint) on Aug 23, 2008 at 07:54 UTC
Re: I can't see "our" variables from outside the package
by Anonymous Monk on Aug 23, 2008 at 07:57 UTC