Solved

Static variables across PERL object oriented classes

Posted on 2006-07-03
3
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
Six Sigma Control Plans

718 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