Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Appconfig module question

by hveneticus (Novice)
on Dec 15, 2017 at 15:34 UTC ( [id://1205595]=perlquestion: print w/replies, xml ) Need Help??

hveneticus has asked for the wisdom of the Perl Monks concerning the following question:

I have to read a configuration file that contains lines like these ones:

VAR=/path/to/ AMB=cer DIRSKE=${VAR}${AMB} SCRIPT=${DIRSKE}/script.sh

After a search, I found that module Appconfig can be the right one. I've read its documentation more and more, but I think I don't really understand how it works (honestly, it is a bit harder to read). Consider also that I'm only a beginner in Perl. My problem is the variable named DIRSKE. Appconfig can expand the value as in a shell script, but I can't understand how do it when I have two of them in the same line.

#!/usr/bin/perl use strict; use warnings; use AppConfig qw(:expand); my $config = AppConfig->new({ CASE => 1, GLOBAL => {EXPAND => EXPAND_ALL} }); $config->define("VAR=s"); $config->define("AMB=s"); $config->define("DIRSKE=s"); # Maybe this is wrong, but what else? $config->define("SCRIPT=s"); #read configuration file $config->file("./file.conf"); #some print print "Valore VAR -> ".$config->VAR()."\n"; print "Valore AMB -> ".$config->AMB()."\n"; print "Valore DIRSKE -> ".$config->DIRSKE()."\n"; print "Valore SCRIPT -> ".$config->SCRIPT()."\n";

The output is:
Valore VAR -> /path/to/ Valore AMB -> cer Valore DIRSKE -> Valore SCRIPT -> /script.sh

I'd like to have something like:
Valore VAR -> /path/to/ Valore AMB -> cer Valore DIRSKE -> /path/to/cer Valore SCRIPT -> /path/to/cer/script.sh

Am I using the correct module?
If yes, can I have a suggestion on how to do it?

Thanks!

Replies are listed 'Best First'.
Re: AppConfig module question
by toolic (Bishop) on Dec 15, 2017 at 16:26 UTC
    As the POD for AppConfig states, the ${var} syntax will expand an environment variable. To expand a variable set in your file, you need to use the $(var) or $var syntax. If you can change your file syntax, you can use this module. If you can't change your file syntax, you can probably hack the module source code to do what you want, or you can pre-process the file to delete {}.
Re: Appconfig module question
by poj (Abbot) on Dec 15, 2017 at 17:07 UTC

    As Toolic suggested, pre-processing the file works

    #!/usr/bin/perl use strict; use AppConfig qw(:expand); use Data::Dump 'pp'; my $config = AppConfig->new({ CASE => 1, GLOBAL => {EXPAND => EXPAND_ALL} }); $config->define("VAR=s"); $config->define("AMB=s"); $config->define("DIRSKE=s"); $config->define("SCRIPT=s"); open my $fh,'<','./file.conf' or die "$!"; my $file = do{ local $/; <$fh> }; $file =~ s/[{}]//g; close $fh; open FH,'<',\$file; $config->file(\*FH); pp $config->{'FILE'}{'STATE'}{'VARIABLE'};;
    poj
Re: Appconfig module question
by karlgoethebier (Abbot) on Dec 15, 2017 at 16:57 UTC
    "...using the correct module..."

    I don't know. But perhaps you can abuse another? Some untested stuff:

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: Appconfig module question
by hveneticus (Novice) on Dec 15, 2017 at 17:23 UTC

    Even three interesting ideas! I'll try them ASAP.
    Many thanks, guys

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1205595]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-19 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found