Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Sharing variables across scripts.

Reply
Thread Tools

Sharing variables across scripts.

 
 
Darren Dunham
Guest
Posts: n/a
 
      10-13-2003
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.

--
Darren Dunham http://www.velocityreviews.com/forums/(E-Mail Removed)
Unix System Administrator Taos - The SysAdmin Company
Got some Dr Pepper? San Francisco, CA bay area
< This line left intentionally blank to confuse you. >
 
Reply With Quote
 
 
 
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      10-13-2003
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
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sharing variables across mulitple files awhan.iitk@gmail.com C++ 5 05-07-2008 08:39 AM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Sharing variables across methods David Garamond Ruby 16 10-14-2004 01:34 PM
Sharing variables across 2 Applications Mothish K ASP .Net 3 06-15-2004 08:34 AM
Sharing Session variables across applications Cowboy \(Gregory A. Beamer\) ASP .Net 4 12-19-2003 06:49 AM



Advertisments