|
Hello,
I am a newbie to object oriented perl. I am having problem to declare a global variable which can be accessible in 2 different functions.
Can anyone help me to declare the "$memory" variable so that it can be seen by both functions?
#!/usr/local/bin/perl -w
package router;
use strict;
sub new
{
my $class = shift;
my $self = {};
bless $self, $class;
return $self;
}
sub func1
{
if ($mode eq "user")
{
my @User = "SELECT count(1) FROM `modeU`";
$memory = $User[0][0];
}
elsif ($mode eq "network")
{
$Network="SELECT count(1) FROM `modeN`";
$memory = $Network[0][0];
}
}
sub func2
{
$memory="INSERT INTO `router`(error) VALUES (\"$memory\")";
}
|
|
|
If you haven't read this please do. http://perldoc.perl.org/perlobj.html
a tip Perl OO doesn't use "#!/usr/local/bin/perl -w"
package router;
is fine and at the end of the file Please use "1;"
you must have the file return a 1
add in a subroutine called DESTROY {}
its for the OO
for your variable you can eather use " my $memory = ''; "
at the top then have a function that returns it like.
sub get_memory {
my $self = shift;
return $memory;
}
or at the top you can do
our $memory = '';
but then that string can be changed form the out side like
use router;
print $router::memory;
$router::memory = 'stuff';
print $router::memory;
my $router_OO = new router;
OR Just return the value after the subroutine is done.
return "some stuff";
then you would get the value like
use router;
my $router_OO = new router;
my $stuff1 = $router_OO->func1();
my $stuff2 = $router_OO->func2();
hope that helps.
|
|
|
|
|
|
|
// |