in reply to Re: Recover a path from a config file
in thread Recover a path from a config file

Hello Corion. Thanks for reply

I thought about your approach but i cannot modify my config.file

So i have to do differently.

*****Lost in translation****TIMTOWTOI****
  • Comment on Re^2: Recover a path from a config file

Replies are listed 'Best First'.
Re^3: Recover a path from a config file
by haukex (Archbishop) on Jun 06, 2016 at 12:14 UTC

    Hi Chaoui05,

    i cannot modify my config.file

    With Perl you can ;-)

    There are several other ways to solve this (maybe even PadWalker), and personally I think the following is a hack that's not very nice, but it works as long as your config file only contains one declaration of my $folder...

    my $conf_str = do { open my $fh, '<', 'work.conf' or die $!; local $/; <$fh> }; # slurp ( $conf_str =~ s/\bmy\s*\$folder\b/our \$folder/g )==1 or die q{Failed to match "my $folder" exactly once}; our $folder; my $config = eval $conf_str;

    Hope this helps,
    -- Hauke D

      I meant i have to not modify my config.file. I have to keep it unchanged.
      *****Lost in translation****TIMTOWTOI****

        Hi Chaoui05,

        I meant i have to not modify my config.file. I have to keep it unchanged.

        The code I showed doesn't modify the config file. It reads the contents of the config file into a Perl string and then modifies that string, leaving the file on disk unchanged.

        Hope this helps,
        -- Hauke D

Re^3: Recover a path from a config file
by GotToBTru (Prior) on Jun 06, 2016 at 12:26 UTC

    If you cannot modify the config file, then none of the suggestions here will help. Perhaps you could use eval instead of do?

    use strict; use warnings; use Data::Dumper; my ($content); { open my $fh,'<','work.conf' or die $!; local $/; $content = <$fh>; } my $config = eval $content; print Dumper(\$config);
    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

      Hello. Thanks for reply.

      As i told below right now, my issue is to get my path. Because i can read and open my config.file. That is not a problem.

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

        It makes the most sense to do more than open the config file. It makes the most sense to create the Perl object the contents of the config file is meant to represent. I've shown you how to do that without modifying the contents of your config file. The information you seek is now in Perl memory. You need to learn how to use hash references to access the particular elements of the config information. I suggest References quick reference as an excellent starting point. See also the following Excellent Hash Reference.

        Your latest update introduces a wrinkle - the contents of the licence tag could be a single item or a list. You will need to learn about the ref command which will tell you which of those scenarios your current config file contains.

        But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)