Solved

mechanism in perl to read config-file

Posted on 2003-10-29
9
718 Views
Last Modified: 2013-11-18
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
0
Comment
Question by:witty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 20

Expert Comment

by:jmcg
ID: 9642981
One common method for writing configuration files for Perl scripts is to have the file consist of executable Perl statements like this example of yours. It's convenient but not secure.

To execute the statements contained in the file, you would typically use a 'require' or 'do' statement.

do "config-file.txt";
0
 
LVL 2

Expert Comment

by:ultimatemike
ID: 9643057
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;
0
 
LVL 1

Expert Comment

by:vermeylen
ID: 9649240
The module Config::Inifiles should to this:
http://search.cpan.org/~wadg/Config-IniFiles-2.38/IniFiles.pm
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:witty
ID: 9650977
@jmcg:
this sounds as the easiest one.

But variables defined in this config-file are undefined in the perl-script!

any idea

thx
michi
0
 
LVL 20

Expert Comment

by:jmcg
ID: 9652889
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;' ???
0
 
LVL 1

Author Comment

by:witty
ID: 9653608
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
0
 
LVL 20

Accepted Solution

by:
jmcg earned 50 total points
ID: 9655253
Well, it _is_ a restriction of "do" and "require" that they are separate lexical scopes. Variables declared with "my" in one file are not visible to code in another file. If you change that "my" to "our", things work the way you expect.
0
 
LVL 1

Author Comment

by:witty
ID: 9662646
"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
0
 
LVL 20

Expert Comment

by:jmcg
ID: 9663205
$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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question