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

I want to know if there's a module or resource to make a variable global for all the script, not only the scope, including inside packages!

I know that our() can be used, but this is only for the same scope. I want to make exactly what happens with %ENV, %INC and @INC.

I have tried to make this changing the Perl source, in the file gv.c, and works fine, but this isn't portable, since Perl binarys need to be recompiled.

For who want to make some tests, here are the changes inside gv.c that I made, this start in the line 712 (Perl-5.8.0), my new global variable is %HWX:

else if (*name == 'H' && strEQ(name, "HWX")) global = TRUE;

Graciliano M. P.
"The creativity is the expression of the liberty".

Replies are listed 'Best First'.
Re: How to make global variable, like %ENV
by broquaint (Abbot) on Oct 07, 2002 at 16:06 UTC
    I asked a similar question in the past and found that you can indeed have truly global variables.
    HTH

    _________
    broquaint

Re: How to make global variable, like %ENV
by BrowserUk (Patriarch) on Oct 07, 2002 at 16:06 UTC

    Update: Seems I misunderstood the question. Sorry.

    Take a look at the use vars; pragma.


    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
      This is not what I want. The use vars allow the access to the variable from other packages, but I still need to declare the package name with the variable: %foo::MYGLOBAL

      Wha I want is the same that happens with %ENV. When I say $ENV{foo} Perl always look in: %main::ENV.

      The nearest resource to do that is the our(), but this only works in the same scope. If you make a require of a file, inside it the previous our() doesn't work!

      But, thanks for the reply!

      Graciliano M. P.
      "The creativity is the expression of the liberty".

Re: How to make global variable, like %ENV
by Aristotle (Chancellor) on Oct 09, 2002 at 19:10 UTC
    Not to be nosy, but what do you need this for? It's likely that you should use a different approach than creating a package-spanning global. If you tell us what you need it for, maybe someone can suggest a better idea.

    Makeshifts last the longest.

      Is for the HWX enverioment. Since the HWX apps can be runed in many packages in the same time, I can't export %HWX to %foo::HWX, because when the app goes out I need to read the hash %HWX to save in the disk, and I can do this if I have a lot of clones of the hash! And I don't want that the user write: %main::HWX, since the hash need to have the idea of a global variable. The changes that I made in the gv.c works very well! What I want is to enable this in other Perl versions, because if someone want to port the resource of HWXperl to other Perl, it will be free to do.

      Graciliano M. P.
      "The creativity is the expression of the liberty".