witty
asked on
mechanism in perl to read config-file
Hallo!
I have a config-file like:
--cut--
$filename="file.txt";
$servername="srv01";
...
--cut--
How to include this in a perl-script, so that I can access these variables from the perl-script?
is there a built-in mechanism to do this very simple?
thx
michael
I have a config-file like:
--cut--
$filename="file.txt";
$servername="srv01";
...
--cut--
How to include this in a perl-script, so that I can access these variables from the perl-script?
is there a built-in mechanism to do this very simple?
thx
michael
Along the lines of jmcg's answer, you could try: (This is assuming your config file is called data.dat). As jmcg mentioned, this is probably not a very safe way of doing this, but depending on your situation, it may be fine.
As well, if you didn't use strict, you wouldn't need to declare the variables before the eval. That would likely lead to some sloppy and buggy code though.
use strict;
my $filename;
my $servername;
open FH, "data.dat";
while (<FH>) {
eval;
}
print $filename;
print $servername;
As well, if you didn't use strict, you wouldn't need to declare the variables before the eval. That would likely lead to some sloppy and buggy code though.
use strict;
my $filename;
my $servername;
open FH, "data.dat";
while (<FH>) {
eval;
}
print $filename;
print $servername;
The module Config::Inifiles should to this:
http://search.cpan.org/~wadg/Config-IniFiles-2.38/IniFiles.pm
http://search.cpan.org/~wadg/Config-IniFiles-2.38/IniFiles.pm
ASKER
@jmcg:
this sounds as the easiest one.
But variables defined in this config-file are undefined in the perl-script!
any idea
thx
michi
this sounds as the easiest one.
But variables defined in this config-file are undefined in the perl-script!
any idea
thx
michi
You don't have a 'Package' directive in this config file, do you?
Or are you saying that the 'do' gets complaints from the perl compiler because you have said 'use strict;' ???
Or are you saying that the 'do' gets complaints from the perl compiler because you have said 'use strict;' ???
ASKER
run.pl:
--cut--
use strict;
my $var=":-(";
do "run.cfg";
print "$var\n";
--cut--
run.cfg:
--cut--
$var=":-)";
--cut--
"perl run.pl" returns ":-("
do I have to code something else in "run.cfg"
thx
michi
--cut--
use strict;
my $var=":-(";
do "run.cfg";
print "$var\n";
--cut--
run.cfg:
--cut--
$var=":-)";
--cut--
"perl run.pl" returns ":-("
do I have to code something else in "run.cfg"
thx
michi
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
"our" works fine!!!
but can anyone explain why:
use strict;
my $var=":-(";
do "run.cfg";
print $main::var."\n";
=> ":-)"
BUT:
use strict;
my $var=":-(";
do "run.cfg";
print $var."\n";
=> ":-("
thx
michi
but can anyone explain why:
use strict;
my $var=":-(";
do "run.cfg";
print $main::var."\n";
=> ":-)"
BUT:
use strict;
my $var=":-(";
do "run.cfg";
print $var."\n";
=> ":-("
thx
michi
$mail::var accesses the global $var variable in your first example. By giving the fully-qualified name, you bypass the lexical $var that was declared with "my".
The second example is still getting the lexical.
The second example is still getting the lexical.
To execute the statements contained in the file, you would typically use a 'require' or 'do' statement.
do "config-file.txt";