Sharing variables across scripts.

Discussion in 'Perl Misc' started by Darren Dunham, Oct 13, 2003.

  1. 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
    Unix System Administrator Taos - The SysAdmin Company
    Got some Dr Pepper? San Francisco, CA bay area
    < This line left intentionally blank to confuse you. >
     
    Darren Dunham, Oct 13, 2003
    #1
    1. Advertising

  2. 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$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Oct 13, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cowboy \(Gregory A. Beamer\)

    Sharing Session variables across applications

    Cowboy \(Gregory A. Beamer\), Dec 18, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    6,375
    Alvin Bruney
    Dec 19, 2003
  2. Mothish K

    Sharing variables across 2 Applications

    Mothish K, Jun 15, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    379
    Patrice
    Jun 15, 2004
  3. Replies:
    5
    Views:
    306
    dizzy
    May 7, 2008
  4. David Garamond

    Sharing variables across methods

    David Garamond, Oct 13, 2004, in forum: Ruby
    Replies:
    16
    Views:
    226
    Jim Weirich
    Oct 14, 2004
  5. rajendra

    sharing a variable across two scripts.

    rajendra, Nov 29, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    161
    Darren Dunham
    Nov 30, 2006
Loading...

Share This Page