0

I know ,I can use Exporter and @EXPORT_OK/@EXPORT to use other module's subroutine,but how can I access other module's variable? Another question , in perl ,is there a difference between static variable and non-static variable?In other words, can I access other module's variable by both the module name and the module reference?

For example Module MyModule.pm;

package MyModule;
our $tmp=1;
sub new{
$this={};
bless $this;
return $this;
}
1;

perl file test.pl

Use MyModule;

How did I access the $tmp in test.pl? And , If I change $tmp to 2 in test.pl, what's the result if I access it in another perl file temp2.pl?

wuchang
  • 3,003
  • 8
  • 42
  • 66
  • http://stackoverflow.com/questions/3109672/how-to-make-a-hash-available-in-another-module – daxim Jul 05 '13 at 13:05

3 Answers3

4

You can access the variable like this:

$MyModule::tmp

Now that you know how to access the variable, you'll be able to confirm through experimentation that modifications made to the variable in one place will be visible elsewhere.

I'm not sure what your question is regarding static vs. non-static. I do know that distinction is not especially relevant in Perl.

FMc
  • 41,963
  • 13
  • 79
  • 132
2

In fact you can export variables using Exporter too! That said you might not want to. As FMc mentions, you can use the fully qualifed name (with ::) to access package variables in other modules. A package variable is one created with our, this technique will not work with my variables ( and hence the name :-) )

Joel Berger
  • 20,180
  • 5
  • 49
  • 104
0

Don't use variables in Exporter. Write an accessor method instead (and change $tmp to a my variable).

sub get_tmp {
    return $tmp;
}
shawnhcorey
  • 3,545
  • 1
  • 15
  • 17