X
X
xmolex2012-09-08 09:44:25
Perl
xmolex, 2012-09-08 09:44:25

I want global script variables to be seen in the module, is it possible?

I am writing a program with the following architecture:

/global.cgi - инициализирующий скрипт, ловящий переменную с задачей и решающей, какой модуль подгрузить (action.pm)
/lib/module.pm - модуль часто используемых функций
/lib/action1.pm - модуль конкретной задачи
/lib/action2.pm - модуль конкретной задачи
/lib/action3.pm - модуль конкретной задачи

The database stores the settings that global.cgi receives from the database and saves them in the global hash. So, it is necessary that this cache be visible in other modules. Of course, you can pass a reference to the hash in each function, but this is hemorrhoids, because. in theory, it initializes the global.cgi program, which calls the function from action.pm, which uses the functions from module.pm and this hash is needed everywhere.
Previously, this hash was initialized in module.pm and imported, however, the site is running under mod_perl and when a copy of the site was launched with different settings, it turned out that sometimes site 1 received site 2 settings. Most likely mod_perl saw that the lib::module library was already loaded and imported its settings, and they are for example the first site. Because of this, I decided to transfer the settings to the global.cgi root script, but I don’t understand how to transfer the settings to the modules. Perhaps it was possible to simply make lib::module object-oriented and then there would be no problems with mod_perl, but I'm not friends with OOP and I don't know if my judgments are correct. I know, there is just a %ENV that is visible in all modules, so there is hope that this can be implemented. If you have any ideas on how to do this, I would be grateful. If it is not clear written, write, I will try to explain in more detail. Already rummaged through tons of information, but so far to no avail.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
ksusha, 2012-09-08
@xmolex

Make a separate caching module, where you put the variable with the cache and all the standard methods for working with it (get, set, etc.), then put the settings and cached data of different sites in the cache under different keys, and with each click, simply assign the data for the desired key as the current . Those. there will be a global cache for everyone and a cache for a specific site. It is possible to make cache variables available everywhere, but it is better to make them available through the functions of the caching module.
PS The object-oriented nature of the module specifically in this task will not help you in any way.

V
Vyacheslav Golovanov, 2012-09-08
@SLY_G

Well, since the hash with the settings needs to be transferred, it must be transferred.
If the initializer loads modules according to the received commands, this is probably done in one place of the script and it is not clear what the problem is G)
Well, it will pass this reference to the hash as the first parameter ...
If there is OOP, then it will not be passing a function parameter, but a constructor call with initialization by these parameters, in general the form will change.
Alternatively, you can save this hash in a file / database, and load it in modules.

A
afandiman, 2012-09-13
@afandiman

Perhaps this is exactly what you need
perldoc.perl.org/functions/our.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question