?
Solved

Static variables across PERL object oriented classes

Posted on 2006-07-03
3
Medium Priority
?
256 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
1 Comment
 
LVL 25

Accepted Solution

by:
clockwatcher earned 1200 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans
Suggested Courses

809 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