SPUG: geting variables from a 'required' file

Ben Burnett benwa at ocentrix.net
Mon May 14 16:09:31 CDT 2001


Dan,

AFAIK 'our' isn't available pre 5.6.  What I do is put a
"use vars" statement near the beginning of my scripts.
for example:

[ben at mail ben]$ cat mainscript.pl
#!/usr/bin/perl
 
use strict;
use diagnostics;
use vars('$ext_var');
#   ^-- this creates a placeholder in this script's
# (actually this package's) namespace for the variable I'm
# bringing in from the requirescript.
 
my $int_var = "I am internal.";
 
require "./requirescript.pl";
 
print STDOUT "$int_var\n$ext_var\n";
 
[ben at mail ben]$ cat requirescript.pl
$ext_var = "I am external.";
# ^-- When you import this variable it should be scoped to
# mainscript's name space. As long as you don't declare a
# different package before you declare your variables in
# this document

[ben at mail ben]$ ./mainscript.pl
I am internal.
I am external.
[ben at mail ben]$

This method can get unweildy if you have a bunch of
variables that you need to bring in from required code as
each variable needs to be added to the "use vars"
statement.  If I need to do this I usually just require one
variable and make it a hash.  That way I can move variables
from the required file to the main script and back without
having to update my "use vars" statement.

-Ben

------- Original Copy -------
>Subject: SPUG: geting variables from a 'required' file
>Date: 05/14/2001 1:07 PM
>From: Dan Ebert <mathin at mathin.com>
>To: spug-list at pm.org

>
>I am wanting to set some global variables in a required
file (so I can use
>different values on different servers without editing the
main script
>code.)  This code works fine with perl 5.6:
>
>use strict;
>
>our ($var1,$var2);
>require 'vars.pl';
>
>print $var1, "\t", $var2, "\n";
>
>but not with 5.005 and if I use 'my' instead of 'our' the
values of $var1
>and $var2 are NULL.  Is there a way (with 5.005) to declare
the variables
>so this will work and 'use strict' won't complain?
>
>Thanks!
>
>Dan.
>-----------------------------------------------------------
>Optimists:  the glass is half full.
>Pessimists: the glass is half empty.
>Engineers:  the glass is twice as big as it needs to be.
>-----------------------------------------------------------
>
>
>
>
>
>
>
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - -
>     POST TO: spug-list at pm.org       PROBLEMS:
owner-spug-list at pm.org
>      Subscriptions; Email to majordomo at pm.org:  ACTION 
LIST  EMAIL
>  Replace ACTION by subscribe or unsubscribe, EMAIL by your
Email-address
> For daily traffic, use spug-list for LIST ;  for weekly,
spug-list-digest
>  Seattle Perl Users Group (SPUG) Home Page:
http://www.halcyon.com/spug/
>
>



 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/





More information about the spug-list mailing list