Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Update config file parameters

by dbuckhal (Friar)
on Jan 06, 2016 at 22:37 UTC ( #1152125=note: print w/replies, xml ) Need Help??


in reply to Update config file parameters

I understand that your config file is probably plain text, but here's an option using Data::Dumper and require.

Start with a configuration format, like a HASH:
perl -MData::Dumper -e ' $conf = { DEVICE => 'eth0', ONBOOT => 'yes', BOOTPROTO => 'static', TYPE => 'Ethernet', IPADDR => '10.9.0.200', NETMASK => '255.255.0.0', GATEWAY => '10.9.1.254', }; open( $fh, ">", "monkConfig.txt" ) or die; print $fh Data::Dumper->Dump([$conf],["config"]); '
Creates a file with this format:
$config = { 'TYPE' => 'Ethernet', 'ONBOOT' => 'yes', 'NETMASK' => 255.255.0.0, 'GATEWAY' => 10.9.1.254, 'DEVICE' => 'eth0', 'IPADDR' => 10.9.0.200, 'BOOTPROTO' => 'static' };
Then use require to access the data:
perl -le 'require "monkConfig.txt"; print $config->{'TYPE'}; ' __output__ Ethernet
...then redo the Dump to write changes back to file. Yeah, probably a bit more tedious then Tie::File, but imagine the possibilities!

Replies are listed 'Best First'.
Re^2: Update config file parameters
by afoken (Abbot) on Jan 07, 2016 at 21:12 UTC
    here's an option using Data::Dumper and require

    Please DON'T use this "solution"!

    This opens a HUGE vulnerability, simply because require $filename;, use $filename;, do $filename;, and eval $filecontent; all treat the configuration file as executable code.

    See also Re^2: conf file in Perl syntax, Re^2: Storing state of execution.

    Or just imagine someone successfully executing echo 'system "/bin/rm -rf /";' >> monkConfig.txt before root executes perl -le 'require "monkConfig.txt";'.

    Also, use and require load each file only once, unless you start messing with %INC.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Wow, I did not know require would execute the content of the file.

      Very good to know. I am glad you found my post and corrected it.

      Thanks!
        I did not know require would execute the content of the file.

        Think about it: How else could require and use (which is just require wrapped in a BEGIN block, and optionally a call to an import method) load and initialise a perl module? How would module Foo load a module Bar that it depends on, if not by executing use or requrie in Foo when the main program requires or uses Foo?

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1152125]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2018-06-20 15:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (116 votes). Check out past polls.

    Notices?