I am currently updating an old perl website. The site itself runs via a custom package, using CGI::Application

There are many global variables which I'm refactoring out, not least because I want to start using mod_perl.

These globals are defined in a special globals package using vars::global and simply used in the code like $my_global_variable

They comprise of three types of variable; user properties, "static" information like paths or images folder, and page specific information like breadcrumbs.

Luckily, I have a $self object passed to all the different subs that generate website page information. I have created these methods in the main website package:

############### sub set_param { ############### my $self = shift; my ($name, $value) = @_; $self->{$name} = $value; return; } ############### sub get_param { ############### my $self = shift; my ($name) = @_; return $self->{$name} || ''; }
And so I can use the variables like so:
$self->get_param('breadcrumb');
This work perfectly, but before doing the major refactoring work, I wanted to check that I'm on the right track. Essentially, my question is whether this is the accepted/best practice for dealing with these kind of variables, which are needed throughout the code (i.e. in the subs for page types and also in the main package itself which deals with rendering the page and the master template for all pages).

Also, for the "static" variables that don't change in the code, I can do it the same way as above, but is there a better way i.e. so they don't have to be re-populated within the same thread once I start using mod_perl.

Thanks


In reply to Best Practice for Replacing Globals by beaker121

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.