[Raleigh-talk] Sharing data between modules

Andrew Rankin andrew at eiknet.com
Fri Dec 19 07:04:15 PST 2008


Hi All.

I have question about sharing some data between some Perl modules and
the best way to accomplish it.

I have written an application for tracking our IBM bladecenters and
Xen VMs, basically it gives you a list of "containers" (the Domain-O
and the Blade centers) and lets you list the servers and information
on those servers within that container:
http://eiknet.com/glovebox01.png.  The backend is perl (obviously :)).
 My issues is that I have a set of data that I'd like to share with
the applications perl modules, they are layed out like this:

                                                  ->
Interfaces::VIRT.pm -> Shared::Library & DB
AJAX -> index.pl -> Glovebox.pm -> Interfaces::SNMP.pm -> Shared::Library & DB
                                                  -> Shared::Library.pm
                                                  -> DB

Within Glovebox.pm's new() I open a database connection, collect the
cgi paramaters, a list of interfaces and open the shared library.
Which looks something like this:

$VAR1 = {
                 'interfaces' => {
                                   'bladecenters' => {
                                                       'deldev' => '0',
                                                       'name' =>
'BladeCenter SNMP',
                                                       'id' => '1',
                                                       'class' =>
'Glovebox::Interface::SNMP',
                                                       'type' => 'bladecenters',
                                                       'enabled' => '1'
                                                     },
                                   'virt' => {
                                               'deldev' => '1',
                                               'name' => 'Xen Dom-0',
                                               'id' => '2',
                                               'class' =>
'Glovebox::Interface::VIRT',
                                               'type' => 'virt',
                                               'enabled' => '1'
                                             }
                                 },
                 'db' => {
                           'database' => 'systems',
                           'lock_id' => 8797996,
                           'db_user' => 'theuser',
                           'dsn' => 'DBI:mysql:database=systems;host=localhost',
                           'db_hostname' => 'localhost',
                           'dbh' => bless( {}, 'DBI::db' ),
                           'db_password' => 'notreallythepw'
                         },
                 'DevLib' => bless( {
                                      'db' => {
                                                'database' => 'systems',
                                                'lock_id' => 7291450,
                                                'db_user' => 'theuser',
                                                'dsn' =>
'DBI:mysql:database=systems;host=localhost',
                                                'db_hostname' => 'localhost',
                                                'dbh' => bless( {}, 'DBI::db' ),
                                                'db_password' =>
'notreallythepw'
                                              },
                                      'container' => undef
                                    }, 'Glovebox::Shared::DevLib' ),
                 'param' => {
                              'type' => 'virt'
                            }
               };


I'd like to share that data between all the modules to avoid the
multiple database connections and multiple shared library's being
opened. Whats the best way of doing this?  So far I've had suggested
using Exporter or defining a variable using 'our' in Glovebox.pm and
just referencing back to it.  Thoughts?

- Andrew Rankin


More information about the Raleigh-talk mailing list