Sharing variables across scripts.

D

Darren Dunham

I have a set of about 5 or 6 scripts related to a single task. Rather
than have each one define all the variables they need in common, I want
to have them load a "header" file defining them.

Is this a good use of Exporter? When I read over the documentation, I'm
left with the idea that I'm overlooking an easier or better idea.

Something like...

<variables>
package LocalPackage;
[...]
use Exporter;
@EXPORT = qw($top_dir, $bin_dir, $process_user);
$top_dir="/path/to/data";
1;

<script>
use LocalPackage;
package LocalPackage;
[...]
chdir $topdir;

Is this worse than just defining the variables and 'require'ing the
file?

The scripts already exist, they're not OO, I'm not going to rewrite
them. I just want to make it easier to have the shared variables in one
file rather than independently defined in several places.

Thanks.
 
T

Tassilo v. Parseval

Also sprach Darren Dunham:
I have a set of about 5 or 6 scripts related to a single task. Rather
than have each one define all the variables they need in common, I want
to have them load a "header" file defining them.

Is this a good use of Exporter? When I read over the documentation, I'm
left with the idea that I'm overlooking an easier or better idea.

No, using the Exporter is fine here. require() or even do() could be
used as well, but they are not quite as flexible.
Something like...

<variables>
package LocalPackage;
[...]
use Exporter;
@EXPORT = qw($top_dir, $bin_dir, $process_user);
$top_dir="/path/to/data";
1;

Does that work at all? I was under the impression that your exporting
module needs to be derived from Exporter. Therefore:

package LocalPackage;
use base qw(Exporter);
@EXPORT = qw(...);
...
1;
<script>
use LocalPackage;
package LocalPackage;
[...]
chdir $topdir;

Is this worse than just defining the variables and 'require'ing the
file?

No, it isn't. It has the additional advantage that you have some nice
control over what is exported. For instance, you could import variables
on request only by putting them into @EXPORT_OK:

package LocalPackage;
use base qw(Exporter);

# none of those are now exported by default
@EXPORT_OK = qw($top_dir $bin_dir $process_user);
...

And in your scripts:

# only import the variables you need
use LocalPackage qw($top_dir $bin_dir);

A related note: You should be using strictures in your code:

use strict;
use LocalPackage;
use vars qw($top_dir $bin_dir $process_user);

This requires to pre-declare the global variables you are going to use,
either through 'use vars' or our() which is available ever since
Perl5.6.0.

Tassilo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top