Quoth Pawel said:
I am searching tutorials since 3 hours but no success to my problem.
I have a module with lexical defined:
#mymodule.pm:
Lowercase module names are reserved for pragmas (modules which alter the
behaviour of the Perl parser in some way, list strict and warnings).
You'd be better off calling it MyModule.pm.
You also need a package statement; otherwise your module is not modular,
and you are just writing a perl4-style include file. Please read perldoc
perlmod before trying to write a module.
my $lex;
sub fun {
print $lex;
}
I have another perl file that uses mymodule.pm
main.pl:
use strict; # this would have warned you something was wrong
use warnings; # this is generally useful
This variable is not the same as the my variable in your module file. It
is a global variable that happens to have the same name. Strictures
would have prevented you from using such a global without declaring it,
as this is (almost always) bad programming practice.
fun(); # it does not print 34
My question is - how can I make it ?
The point of lexicals ('my' variables), the reason they are a good
thing, is that they cannot be accessed outside of the scope they were
declared in. This means that you can see from looking at the code where
that variable is being used: there are no 'secret' uses somewhere you
didn't think to look.
A file is a scope in itself. That is, any lexical cannot be accessed
outside of the file it is declared in. The only variables which can be
used across files are package global variables. You will need to read
perlmod to understand these.
Ben