in reply to Recover a path from a config file

Using do or eval to parse files like your work.conf is potentially very dangerous. Arbitrary Perl code can be inserted in files like that.

I have used Data::Undump with some success to more safely read data from files like your work.conf

However, since you are interested in only the values for $folder and license, you could extract those with regular expressions

(Untested)

use strict; use warnings; my $dir; my $lic; my $cfgf = 'work.conf'; open my $cfgh, '<', $cfgf or die "Can't open $cfgf: $!\n"; while (<$cfgh>) { if (/\$folder\s+=\s+'([^']+)'/) $dir = $1; next; } if (/license\s+=>\s+'([^']+)'/) { $lic = $dir . $1; last; } } print "Folder: $dir\nLicense: $lic\n";

Replies are listed 'Best First'.
Re^2: Recover a path from a config file
by Chaoui05 (Scribe) on Jun 07, 2016 at 08:24 UTC

    Hello ,

    Thanks for your approach. It's a very interesting to use regex . I didn't think about that even if i have to learn more about them.

    Concerning do i use, i have to employ that to read my file.

    I didn't know it can be dangerous to use do or  eval. The advantage of my conf.file is the format because it uses Perl.

    *****Lost in translation****TIMTOWTOI****

      The advantage of my conf.file is the format because it uses Perl.

      Think of it like a perl program, because it is a perl program, use/require/do/eval, they all run perl programs like "perl"

      The assumptions/conventions are different, but its full perl interpreter, unrestrained

        Yes Anonymous Monk

        Iam thinking this conf.file like a perl program because it is.

        Thanks

        *****Lost in translation****TIMTOWTOI****