Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Get variables from txt file

by Anonymous Monk
on Aug 28, 2014 at 16:15 UTC ( [id://1098882]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I have written a perl script but I don't know how to get the variables from a txt file. Actually, I can read the txt file in hash and get the variables but I have a small issue. In the txt file I have some paths that include variables.

The txt file with the variables looks like:

V1=/home/user V2=$V1/test V3=$V2/file.txt

I read the lines using:

Read variables open my $vars, '<:utf8', "$Bin/vars.txt" or die $! ; while (<$vars>) { chomp; my ($key, $value) = split(/=/, $_); $vars{$key} = $value; } my $V1 = $vars{'V1'}; my $V2 = $vars{'V2'}; my $V3 = $vars{'V3'}; print "$V1\n"; print "$V2\n"; print "$V3\n"; close ($vars);

The result that I get is:

/home/user /$V1/test /$V2/file.txt

but I want

/home/user /home/user/test /home/user/test/file.txt

I would appreciate any help.

Replies are listed 'Best First'.
Re: Get variables from txt file
by davido (Cardinal) on Aug 28, 2014 at 16:49 UTC

    This is going to eventually deteriorate into a bunch of hacks where you provide ways to escape $ characters, and address other complications such as whitespace , or will grow to a full fledged tokenizing / parsing system. You probably don't want to live with the limitations and mess that the first scenario will become, and probably don't want to invest the time in the second scenario.

    Unless you really want to reinvent this wheel because no other wheel out there fills your need well enough, go with a module like Config::General. This distribution provides variable interpolation via Config::General::Interpolated, which is included when you install Config::General. With Config::General, your solution could look like this:

    use strict; use warnings; use Data::Dumper; use Config::General; my $c = Config::General->new( -ConfigFile => \*DATA, -InterPolateVars => 1, ); print Dumper {$c->getall()}; __DATA__ V1=/home/user V2=$V1/test V3=$V2/file.txt

    Here is the output that will produce:

    $VAR1 = { 'V1' => '/home/user', 'V2' => '/home/user/test', 'V3' => '/home/user/test/file.txt' };

    ...and then you're done and can move on to the real stuff. ;)


    Dave

Re: Get variables from txt file
by MidLifeXis (Monsignor) on Aug 28, 2014 at 16:32 UTC

    See also modules in the Config namespace. Quite a few of them will expand variables found in settings.

    --MidLifeXis

      To add to this, I've been using Config::YAML for quite some time without a hitch. I've been told that it can get screwy when dealing with other implementations due to the spec being... I think robust would be a good term... but for this specific use, it's great.
Re: Get variables from txt file
by toolic (Bishop) on Aug 28, 2014 at 16:28 UTC
    Substitute variable names with previously read values. This requires V1 to have been defined before V2, etc.:
    use warnings; use strict; my %vars; while (<DATA>) { chomp; s/\$(\w+)/exists $vars{$1} ? $vars{$1} : die "Error: no env var $1 +\n"/ge; my ($key, $value) = split(/=/, $_); $vars{$key} = $value; } use Data::Dumper; $Data::Dumper::Sortkeys=1; print Dumper(\%vars); __DATA__ V1=/home/user V2=$V1/test V3=$V2/file.txt

    Outputs:

    $VAR1 = { 'V1' => '/home/user', 'V2' => '/home/user/test', 'V3' => '/home/user/test/file.txt' };

      Hi,

      I have tried your solution and it works! Probably, the other solutions will work! Thank you for your time.

        There are several modules which will interpolate variables into strings so you needn't roll your own. See String::Interpolate

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-24 02:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found