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

Hi guys, i would like to know how to import a variable from a module and use it globally? Thanx a lot

Replies are listed 'Best First'.
Re: how to import a module var
by Perl Mouse (Chaplain) on Nov 22, 2005 at 12:23 UTC
    The easiest way is for the module to export the variable using Exporter. For instance:
    package MyModule; require Exporter; our @ISA = qw /Exporter/; our @EXPORT_OK = qw /$some_variable/; ... 1; # Program using the module. package main; use MyModule '$some_variable';
    Perl --((8:>*
      hi, i already used the Exporter but still my prog cant access the arra +y in the module. Here is my module : package Voucher; use strict; use warnings; require Exporter; our @ISA = qw /Exporter/; our @EXPORT_OK = qw /@LIST @RANGE1 @RANGE2 @RANGE3 @RANGE4 @RANGE5 @RA +NGE6/; sub new { my ($class) = @_; my $self = { _loadSch => undef, _loadRanges => undef }; bless $self,$class; return $self; } sub loadSch { my ($self,$loadSch) = @_; my $schdir="./D_sch_folder/"; opendir(SCH,$schdir) || die "Error::Cannot load Sch Directory!!!\n +"; my @sch=grep(/[dD]_[sS][cC][hH].txt/,readdir(SCH)); my $schlist = $schdir.$sch[0]; open (IN,"$schlist") || die "Error::Cannot open Sch list!!!\n"; print "Loading d_sch.txt . . .\n\n"; our @LIST=<IN>; my $c=@LIST; print "$c\n"; sleep 1; } sub loadRanges { my ($self,$loadRanges) = @_; my $rangedir = "./number ranges/"; my $loadCTU7 = $rangedir."CTU7_range.txt"; my $loadTU7 = $rangedir."TU7_range.txt"; my $loadCTU30= $rangedir."CTU30_range.txt"; my $loadTU30 = $rangedir."TU30_range.txt"; my $loadDIAZ = $rangedir."DIAZ_ranges.txt"; my $loadIDD = $rangedir."IDD_range.txt"; open(R1,"$loadCTU7") || die "Error::Cannot load CTU7 ranges!!!\n"; open(R2,"$loadTU7") || die "Error::Cannot load TU7 ranges !!!\n"; open(R3,"$loadCTU30") || die "Error::Cannot load CTU30 ranges!!!\n +"; open(R4,"$loadTU30") || die "Error::Cannot load TU30 ranges!!!\n"; open(R5,"$loadDIAZ") || die "Error::Cannot load DIAZ ranges!!!\n"; open(R6,"$loadIDD") || die "Error::Cannot load IDD ranges!!!\n"; print "Loading E-Mongolia Card Ranges . . .\n\n"; my @RANGE1=<R1>; my @RANGE2=<R2>; my @RANGE3=<R3>; my @RANGE4=<R4>; my @RANGE5=<R5>; my @RANGE6=<R6>; sleep 1; } 1; <br> and here is my code: #! /usr/perl/bin/perl use warnings; use lib qw (./modules); use Voucher; $vouch=Voucher->new(); $vouch->loadSch(); # load d_sch.txt $vouch->loadRanges(); # load ranges $count=$pro1=$pro2=$pro3=$pro5=$pro6=$pro7=$pro8=$pro19=$pro20=$pro21= +$pro22=$pro23=$pro24=$pro25=$pro27=0; $l=@LIST; print "$l\n"; foreach $in (@LIST){ if ($in=~/^922/){ chomp($in); print "$in\n"; @a=split(/\s+/,$in); if ($a[4] eq "PROFILE1"){ $pro1++; $count++; } elsif ($a[4] eq "PROFILE2"){ $pro2++; $count++; } elsif ($a[4] eq "PROFILE3"){ $pro3++; $count++; } elsif ($a[4] eq "PROFILE5"){ $pro5++; $count++; } elsif ($a[4] eq "PROFILE6"){ $pro6++; $count++; } elsif ($a[4] eq "PROFILE7"){ $pro7++; $count++; } elsif ($a[4] eq "PROFILE8"){ $pro8++; $count++; } elsif ($a[4] eq "PROFILE19"){ $pro19++; $count++; } elsif ($a[4] eq "PROFILE20"){ $pro20++; $count++; } elsif ($a[4] eq "PROFILE21"){ $pro21++; $count++; } elsif ($a[4] eq "PROFILE22"){ $pro22++; $count++; } elsif ($a[4] eq "PROFILE23"){ $pro23++; $count++; } elsif ($a[4] eq "PROFILE24"){ $pro24++; $count++; } elsif ($a[4] eq "PROFILE25"){ $pro25++; $count++; } elsif ($a[4] eq "PROFILE27"){ $pro27++; $count++; } } } print "$count\n";
        You make some variable potentially available by listing them in @EXPORT_OK, but you never import then when using the module. Either make the variable available by default, using @EXPORT instead of @EXPORT_OK, or tell the import routine which variables you want, by listing them in the use statement.

        And next time, don't post a gazillion lines - you could have written a short example with the same problem.

        Perl --((8:>*
        You need to perldoc Exporter
Re: how to import a module var
by tirwhan (Abbot) on Nov 22, 2005 at 12:26 UTC

    Unless the variable is exported (in which case you can import it with use Module qw($variable)), this is probably a bad idea, because you're accessing a module internal which can change its behaviour at any given time, thereby breaking your code. The module has an interface for a reason, use it. If the module does not give you a specific bit of information that you need, write a patch and add a method. If the module author is not willing to accept your patch, that's a pretty good indicator that there's a better way of doing what you want.


    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
      I want to second the above. It's always dodgy to export anything from an OO module. If your module has a method called new, you need a very good reason for it to export anything. If outsiders need to access it, make a method for them to use.

      This is especially important when cross language programs come along. When Perl is tied to Java or Python, those other languages can't see the exports, they need methods to call.

      Perhaps the only valid use of exports in the OO context is for mixin modules which provide exported subs that lots of other classes can import into their packages. But those exporting mixin providers don't have constructors. Which leaves me back at my rule of thumb: constructors and Exporter don't mix.

      This is not to say that clever OO modules might not have a magical import that does all sorts of mischief (like fabricating whole classes). But they shouldn't actually put anything into the caller's name space like Exporter does.

      Phil

        I sometimes export a constructor. Why? Because sometimes, I rather write:
        my $obj = new_feeble();
        than
        my $obj = Feeble->new();
        The former requires less tokens. ;-)

        And I'll export constants from my OO modules if I feel like it.

        Perl --((8:>*