Sam Weisenthal
Sam Weisenthal

Reputation: 2951

How to change configurations for Perl script on the fly

Is this an acceptable way to reconfigure without exiting the script?

use XML::Simple;

my $config               = "C:/users/config.xml";
my $config               = new XML::Simple->XMLin($config_file);
my $volume               = $config->{volume};

print "volume 1 $volume\n";

sleep 15;#during this time, change config.xml

my $config               = new XML::Simple->XMLin($config_file);
my $volume               = $config->{volume};

print "volume 2 $volume\n";

o/p

volume 1 1
volume 2 0

Upvotes: 3

Views: 73

Answers (1)

Leeft
Leeft

Reputation: 3837

Not the most efficient way, but certainly acceptable. If it's a sizeable configuration file and processing time is relevant you'd want to check the file has been changed before reloading it.

Upvotes: 3

Related Questions