Contents Up Prev Next

3.2.3. Importing Variables

It is also possible to import or export the global variables of different modules. However, such variables need to be declared using the use vars qw($myvar1 @myvar2) construct.

Here's an example for a module that defines a variable and another one that access it:

# This file is MyVar.pm
#
package MyVar;

use strict;

# Declare a namespace-scoped variable named $myvar.
use vars qw($myvar);

sub print_myvar
{
    print $myvar, "\n";
}

1;
#!/usr/bin/perl

use strict;

use MyVar;

$MyVar::myvar = "Hello";

MyVar::print_myvar();

$MyVar::myvar = "World";

MyVar::print_myvar();

Contents Up Prev Next