#564 Perl: The code to manage a global variable is wrong

open
nobody
perl (97)
5
2005-05-25
2005-05-19
Fabrice Salvaire
No

The code to manage a global variable is wrong:

my %__global_hash;
tie %__global_hash,"Package::Class", $Packagec::global;
my $global= \%__global_hash;
bless $global, Package::Class;

When Perl load the module, "$Package::global" is set to
the CURRENT VALUE of the
global variable. After, the "get" stub is called
anymore. If this global variable is
modified in the C code after, the perl variable keep
the initial value.

A possible solution is to replace this code by a function

sub global
{
my %__global_hash;
tie %__global_hash,"Package::Class", $Packagec::global;
my $global= \%__global_hash;
bless $global, Package::Class;
replace $global;
}

and use "Package::globale ()" instead of
"$Package::globale".

Discussion

    • summary: The code to manage a global variable is wrong --> Perl: The code to manage a global variable is wrong
     
  • Logged In: YES
    user_id=1069371

    better is

    sub global
    {
    my %global;
    tie %global, "Package::Class", $Packagec::global;
    bless \%global, Package::Class;
    }