beaker121 has asked for the wisdom of the Perl Monks concerning the following question:
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:
And so I can use the variables like so:############### sub set_param { ############### my $self = shift; my ($name, $value) = @_; $self->{$name} = $value; return; } ############### sub get_param { ############### my $self = shift; my ($name) = @_; return $self->{$name} || ''; }
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).$self->get_param('breadcrumb');
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Best Practice for Replacing Globals
by kcott (Archbishop) on Apr 19, 2023 at 20:08 UTC | |
by LanX (Saint) on Apr 19, 2023 at 22:02 UTC | |
|
Re: Best Practice for Replacing Globals
by marto (Cardinal) on Apr 20, 2023 at 08:18 UTC | |
by hippo (Archbishop) on Apr 20, 2023 at 08:31 UTC | |
by marto (Cardinal) on Apr 20, 2023 at 08:43 UTC | |
by hippo (Archbishop) on Apr 20, 2023 at 13:34 UTC | |
|
Re: Best Practice for Replacing Globals
by tobyink (Canon) on Apr 20, 2023 at 07:21 UTC | |
|
Re: Best Practice for Replacing Globals
by LanX (Saint) on Apr 19, 2023 at 13:40 UTC | |
by marto (Cardinal) on Apr 19, 2023 at 13:43 UTC | |
by beaker121 (Novice) on Apr 19, 2023 at 15:42 UTC | |
by LanX (Saint) on Apr 19, 2023 at 16:00 UTC | |
by beaker121 (Novice) on May 06, 2023 at 21:37 UTC |