Solved

Static variables across PERL object oriented classes

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

896 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

12 Experts available now in Live!

Get 1:1 Help Now