leocharre has asked for the wisdom of the Perl Monks concerning the following question:
So I thought I was writing clean code, friendly to any coders that might want to use parts of it, etc... I sepparated stuff into modules, checked to make sure the namespaces did not collide as i offer them out..
Made sure to comment stuff and name my vars so that any prying eyes would know what they do.. and then.. No. Turns out, my code is third grade.
I found an unforgivable hole in what I do.
I use global variables. Yes.
Here's why.
(this is a web app)
I have Webapp.pm
then i have Webapp/Bathroom.pm
then i have Webapp/Bathroom/Toilet.pm
then i have Webapp/Bathroom/Status.pm
I'm using a globally accessible hashes of data,
%Webapp::Bathroom
which might hold something like
@{$Webapp::Bathroom{toilets}}
and then
%{$Webapp::Bathroom{joetoilet}
%{$Webapp::Bathroom{janetoilet}
I need everything in the Webapp namespace to access the %Webapp::Bathroom data. And I need any module I want to be able to modify data in there as well.
I seem to have been doing it this way.. I declare that Webbapp/Bathroom.pm has an our %Bathroom
and that this symbol is exported.
so, other modules can access and change data in %Bathroom by pointing to %Webapp::Bathroom, without calling use Bathroom
Ok. I seem to be going insane. That's ok. Is this sounding like I should be playing with OO at this point ? maybe making a $Webapp object to store all data into ? How will this be accessible to all dependant packages ? Ouch.. Point to where I should take my corpse to properly do this.. I know I'm not supposed to use our, ever. I don't want to write useless code. Or worse, troublemaking code.
What is a propper way to make a chunk of data accessible to all my packages for retrieval and modification ?
|
|---|