codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Problem to declare global variable in Object oriented perl  veela at 06:56 on Sunday, September 21, 2008
 

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\")";

}

  Re: Problem to declare global variable in Object oriented perl  S_Flex at 13:09 on Sunday, September 21, 2008
 


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.








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  Problem to declare global variable in Object oriented perl
•  Issue with Javascript between web servers
•  Not Allowing Copying from Site
•  Re: dereferenceing a string to a constant name
•  Scrolling Javascript Product Slider
•  Re: TO SEND Text message or URL through Clickatell
•  A little help
•  Re: Storing data from HTML to Excel or TXT
•  Re: Parsing, fish up all between <td> and </td> ... without possibility of confusion.


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2008