newbie - changing value of lexical from outside of module

P

Pawel

I am searching tutorials since 3 hours but no success to my problem.

I have a module with lexical defined:

#mymodule.pm:

my $lex;
sub fun {
print $lex;
}

I have another perl file that uses mymodule.pm

main.pl:
use mymodule;
$lex=34;
fun(); # it does not print 34

My question is - how can I make it ?

Greetings
 
D

Dave

Pawel said:
I am searching tutorials since 3 hours but no success to my problem.

I have a module with lexical defined:

#mymodule.pm:

my $lex;
sub fun {
print $lex;
}

I have another perl file that uses mymodule.pm

main.pl:
use mymodule;
$lex=34;
fun(); # it does not print 34

My question is - how can I make it ?

Greetings

You cannot directly access a lexical outside its scope. Options include
making it a package variabel instead, or (better) providing get / set
subroutines in the module.
 
B

Bart Van der Donck

Pawel said:
#mymodule.pm:

my $lex;
sub fun {
print $lex;
}

I have another perl file that uses mymodule.pm

main.pl:
use mymodule;
$lex=34;
fun(); # it does not print 34

My question is - how can I make it ?

Here are 2 possible ways:

-------------------------------------------

main.pl

#!perl
use strict; use warnings;
use mymodule;
my $lex=34;
mymodule::fun($lex);

mymodule.pm

package mymodule;
sub fun {
print shift;
}
1;

-------------------------------------------

main.pl

#!perl
use strict; use warnings;
use mymodule;
$mymodule::lex=34;
mymodule::fun();

mymodule.pm

package mymodule;
sub fun {
print $lex;
}
1;
 
B

Ben Morrow

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
use mymodule;
$lex=34;

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
 
P

Pawel

Hallo all.

Thank You for all Yor responses. Now it is clear what is the right solution.

Greetings
 
D

Dave Weaver

Here are 2 possible ways:
--- <snip> ---

And here is a 3rd possible way:

main.pl

#!perl
use strict; use warnings;
use mymodule;

mymodule::set_lex(34);
mymodule::fun();

mymodule.pm

package mymodule;
my $lex;
sub set_lex {
$lex = shift;
}
sub fun {
print shift;
}
1;
 
B

Bart Van der Donck

Dave said:
And here is a 3rd possible way:

main.pl

#!perl
use strict; use warnings;
use mymodule;

mymodule::set_lex(34);
mymodule::fun();

mymodule.pm

package mymodule;
my $lex;
sub set_lex {
$lex = shift;
}
sub fun {
print shift;
}
1;

Four :)

main.pl

#!perl
use strict; use warnings; use mymodule;
my $lex = 34; mymodule::fun for $lex;

mymodule.pm

package mymodule; sub fun {print} 1
 
D

Dave Weaver

main.pl

#!perl
use strict; use warnings;
use mymodule;

mymodule::set_lex(34);
mymodule::fun();

mymodule.pm

package mymodule;
my $lex;
sub set_lex {
$lex = shift;
}
sub fun {
print shift;

oops! That should have been:

print $lex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top