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

Greetings all,

I have been trying to work out, for the last few days no less, how to play around with vars between packages.
You know, all of $This::Kind::OfStuff

'Well Documented!' I hear you cry, and right you are... except, I cannot find any reference to how to use a var declared in the main runtime file.
So, I have my main piece of code with hashbang and a stack of use's, this main prog doesn't have any package name (since its not a module I don't bother, of course) -- how do my external packages use vars from the root programme?

Many thanks,
-- JP

Replies are listed 'Best First'.
Re: Who's who in interpackage global vars
by arhuman (Vicar) on Feb 20, 2001 at 20:02 UTC
    $::variable works too...
    @::variable or
    %::variable depending on what you're trying to access

    (Rather useless unless you're planning to do some obfuscation ;-)

    Furthermore I reommend the camel book (chapter 5) to understand why %main::main::main::variable works and is similar to %::variable or %main::main::main::main::main::main::variable (add as many main:: as you want before variable)

Re: Who's who in interpackage global vars
by baku (Scribe) on Feb 20, 2001 at 19:47 UTC

    The main package is main -- as in $main::variable &c.

Re (tilly) 1: Who's who in interpackage global vars
by tilly (Archbishop) on Feb 20, 2001 at 20:53 UTC
    This is good stuff to know, but in practice it is generally better to avoid excessive global variables, and packages which do use global variables should use Exporter to decide which ones to export.

    Poking around in someone else's namespace is quite doable, but I consider it sort of like walking into someone else's house uninvited and browsing around. Rather rude, wouldn't you say?

Re: Who's who in interpackage global vars
by JPaul (Hermit) on Feb 20, 2001 at 20:40 UTC
    Greetings again -- I filed the original question (and then found the "Mail me my password since I'm a doofus and forgot it" function).

    At any rate -- I discovered why I couldn't make the $main::var work (which I had tried) - I use 'use strict'. Check the code:

    use strict; my $fish = "Trout"; print "Works: $fish\n"; print "Not : $main::fish\n";

    Which doesn't work as I'd expect (or would hope it to) at all.
    Can you chaps enlighten me further as to this conundra?

    --JP

      A my variable is not in the package symbol table. Back to the documentation with you :)
Re: Who's who in interpackage global vars
by gaspodethewonderdog (Monk) on Feb 20, 2001 at 20:05 UTC
    you'd use $main::string or &main::function ... you get the idea...

    You could always make the main into a package. Nothing keeps you from doing that either. In fact if you plan on reusing any of the code anyways you might just want to do that for sanity sake.

Re: Who's who in interpackage global vars
by Anonymous Monk on Feb 20, 2001 at 21:34 UTC
    Aha! *Thank you*

    Thats perfect :)

    --JP

Re: Who's who in interpackage global vars
by Anonymous Monk on Feb 20, 2001 at 21:04 UTC
    Good stuff to know - yes - but, forgive me if I find it annoying :P

    I want an external module to be able to use a var from the main package -- and all good perlers use strict, but we can't use the two together :P

    Whats the solution then? Just a simple "don't"?

    -- JP

      Well, define the variable you want to share as a package variable rather than a lexical variable.

      use vars qw($var); $var = 'some value'; &Package::printvar; package Package; sub printvar { print $main::var; }
      --
      <http://www.dave.org.uk>

      "Perl makes the fun jobs fun
      and the boring jobs bearable" - me