Solved

Static variables across PERL object oriented classes

Posted on 2006-07-03
3
245 Views
Last Modified: 2008-02-26
Hi -

I've looked around the web & can't find much on to help me out here, hoping you guys could shed some light.

I have a bit of a perl OO setup that looks like this -

Global
 |
  ---> Security
 |
  ---> Database


Global has basic functions that loads info from a config file such as database connection info, username/passwords, path information etc.

The subclasses Security & Database use this loaded info for various purposes (eg connecting to the database!!)

I create an object for at least Security & Database to keep their functionality separate & clear, and even instantiate Global to get at the loaded path stuff.

The "problem" I'm having is each of these objects naturally reload the data from the server's HDD when they are invoked (the method is called from the constructor), so there's extra overhead there I'd like to eliminate. Its not bad, and certainly not noticeable at the moment, but I think its just bad form =)

I'm thinking it would be nice if I can have static variables across the 'Global' class (& subclasses?), such that any other instances can access the already loaded settings - ???

Is this possible / practical?

Are there any suggestions for a better setup?

I'm thinking an alternative is to pull Security & Database away from Global and have them as top-level classes in themselves, and simply require a handle to a Global object to be passed to it in the constructor ??

Actually as I write this I think that method will be many times better, but I'll post this question anyway to hear any suggestions & see if static variables are possible, for future projects & other curious programmers.

I'll split the points amongst some of the most creative & informative replies.

- Glauron




0
Comment
Question by:Glauron
3 Comments
 
LVL 25

Accepted Solution

by:
clockwatcher earned 300 total points
ID: 17038740
I'm not exactly sure what you're after, but all variables outside the scope of your self anonymous hash should be static.

example.pl
-------------

#!/usr/bin/perl

package obj;
my $static = 3;
$publicstatic = "this is public";
sub new {
        my ($proto, $variable) = @_;
        my $class = ref($proto) || $proto;
        my $self  = {};
        $self->{'instancevar'}   = $variable;
        bless ($self, $class);
        return $self;
}

sub getStatic()
{
      return $static;
}

sub setStatic()
{
       $static = $_[1];
}

package main;
my $obj1 = obj->new("hello");
my $obj2 = obj->new("there");

print $obj1->{'instancevar'}."\n";
print $obj2->{'instancevar'}."\n";

print "obj2 static: ".$obj2->getStatic()."\n";
$obj1->setStatic(2);  # modify static variable from different object
print "obj2 static: ".$obj2->getStatic()."\n";

print "public static: ".$obj::publicstatic."\n";
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question