Configuration question

C

chatiman

Hello,

I'd like to put all the configuration variables in a separate file.
The configuration file will contain etheir a hash:
%conf = ( ... )
or constants :
use constant { ... }

How can I do that in perl without the "dirty":
eval (`cat ...`)
?

Thanks
 
B

Beable van Polasm

chatiman said:
Hello,

I'd like to put all the configuration variables in a separate file.
The configuration file will contain etheir a hash:
%conf = ( ... )
or constants :
use constant { ... }

How can I do that in perl without the "dirty":
eval (`cat ...`)

You could make it into a module. See "perldoc perlmod", and
"perldoc perlmodlib".


--
 
F

fifo

I'd like to put all the configuration variables in a separate file.
The configuration file will contain etheir a hash:
%conf = ( ... )
or constants :
use constant { ... }

How can I do that in perl without the "dirty":
eval (`cat ...`)
?

perldoc -f do
 
C

chatiman

do doesn't reexport variables set from a files if I remember.

I would like a function like this but which does :
eval (`cat ...`)
 
C

chatiman

You mean make a module and than export the variable ... that's nice I didn't
think about it

Thanks

"Beable van Polasm" > You could make it into a module. See "perldoc
perlmod", and
 
U

Uri Guttman

c> do doesn't reexport variables set from a files if I remember.
c> I would like a function like this but which does :
c> eval (`cat ...`)

huh????

what do you think do does? and how is it different than that code (other
than being faster)? and what in the world does 'reexport' mean? and if
you don't remember, look it up in the docs and relearn it.

uri
 
C

chatiman

Well, english is not my native language. I understand it a bit so excuse me
if I misunderstand the following words from perldoc -f do :

""do FILENAME" cannot see lexicals in the enclosing scopre; "eval STRING"
does.

Which I translate (that's probably wrong) as :
- When a file evaluated with "do" sets a variable, you cannot see this
variable from the file that called "do"
.........

Uri Guttman said:
c> do doesn't reexport variables set from a files if I remember.
c> I would like a function like this but which does :
c> eval (`cat ...`)

huh????

what do you think do does? and how is it different than that code (other
than being faster)? and what in the world does 'reexport' mean? and if
you don't remember, look it up in the docs and relearn it.

uri
http://jobs.perl.org
 
B

Ben Morrow

Uri Guttman said:
c> do doesn't reexport variables set from a files if I remember.
c> I would like a function like this but which does :
c> eval (`cat ...`)

what do you think do does? and how is it different than that code (other
than being faster)?

ObPedant:

perldoc -f do:
| except that it's more efficient and concise, keeps track of the
| current filename for error messages, searches the @INC libraries, and
| updates %INC if the file is found.

:)

Ben
 
F

fifo

Well, english is not my native language. I understand it a bit so excuse me
if I misunderstand the following words from perldoc -f do :

""do FILENAME" cannot see lexicals in the enclosing scopre; "eval STRING"
does.

Which I translate (that's probably wrong) as :
- When a file evaluated with "do" sets a variable, you cannot see this
variable from the file that called "do"

No, it means you can't see _lexical_ variables, i.e. those created using
"my". Packages variables, like those in your original example, will be
visible.
 
B

Ben Morrow

[stop top-posting]

chatiman said:
Well, english is not my native language. I understand it a bit so excuse me
if I misunderstand the following words from perldoc -f do :

""do FILENAME" cannot see lexicals in the enclosing scopre; "eval STRING"
does.

Which I translate (that's probably wrong) as :
- When a file evaluated with "do" sets a variable, you cannot see this
variable from the file that called "do"
........

You do misunderstand. What this means is that if you have

file1:
my $var1 = 'hello world';
do 'file2';
print $var1;

file2:
$var1 = 'foo bar';

then the code in file2 will not be able to access $var1 from $file1. If
you are using constants, which are subs, this will not affect you; if
you want variables to pass between files they should be package globals
('our' variables) rather than file lexicals ('my' variables), viz.:

file1:
do 'file2';
print $var;

file2:
our $var = 'hello world';

Note that even when using eval `cat file`, lexicals cannot pass out from
the included file to the including file, that is

file1:
eval `< file2`; # no need for useless-use-of-cat :)
print $var;

file2:
my $var = 'hello world';

will *not* print 'hello world'.

Using a module to hold the config information is better again, though,
as it allows you more control over which bits of config information you
import into your namespace at a given time.

Ben
 
J

Joe Smith

chatiman said:
do doesn't reexport variables set from a files if I remember.

I'd say you remember incorrectly.
do $file or die "Unable to process $file - $!";
Does not import() or export() anything; variables are
added to the current symbol table directly.
-Joe
 

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,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top