-
Recent Posts
Tags
app engines attributes cgi database DBI Debian frameworks full screen terminal GWT hacks hardware hobbit howto html inheritance javascript jQuery libraries Linux mac oracle OSX parallels Perl photoshop pixels PostgreSQL programming qa regression testing screen size Software Development style template toolkit term Theory translation type cast User Interface virtual machine virtual machines visual studio vmware web design web programming
Perl Class Globals
This first post is on Perl, specifically class globals AKA class variables (for Java programmers.) This is mostly so I can figure out how easy it’s going to be to write code on this blog, and to test the server configuration so I can make sure everything uploads OK.
So here’s a contrived example: A class that keeps track of the number of instances of itself.
In Foo.pm
Each time a Foo is instantiated, the class counter gets incremented. This is different than adding a variable to the $self hash, it is stored once per class, not once per instance.
So why would you want to do this? Well the example that came up at work today was an object that has a short (read small in memory) ID that is a key in a larger hash encoding plain English versions of this key. So what we did was store only the short key in $self, and store 1 copy of the translation hash as an object global. The ‘get_key()’ method then looked up the translation and returned the full string, without storing those full strings in a million objects