Solved

Static variables across PERL object oriented classes

Posted on 2006-07-03
3
240 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now