OSDN Git Service

convert to use Any::Config and fix encoding problem
authorISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
Sat, 20 Oct 2012 19:54:35 +0000 (04:54 +0900)
committerISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
Sat, 20 Oct 2012 19:54:35 +0000 (04:54 +0900)
mubot4fb.pl

index e0a70e6..865568c 100755 (executable)
@@ -314,43 +314,51 @@ package main;
 use strict;
 use utf8;
 
-use Config::Simple;
+use Config::Any;
+use Hash::Merge::Simple;
+use Data::Recursive::Encode;
+
+use Data::Dumper;
 
 my $config_name = $ARGV[0] || 'not_found';
 
-my %cfg;
-my @config_path = ('/etc/mubot4fb/', $ENV{HOME} . '/.mubot4fb/', $ENV{PWD} . '/mubot4fb_');
-foreach my $c (@config_path) {
-       my $config = $c . $config_name . '.conf';
-       Config::Simple->import_from($config, \%cfg) if (-e $config);
+my $cfg = {};
+my $config_path = ['/etc/mubot4fb/'.$config_name,
+                  $ENV{HOME} . '/.mubot4fb/'. $config_name,
+                  $ENV{PWD} . '/mubot4fb_' . $config_name];
+
+my $c = Config::Any->load_stems({stems => $config_path,  use_ext => 1, flatten_to_hash => 1});
+foreach my $i (keys %$c) {
+       $cfg = Hash::Merge::Simple->merge($cfg, $c->{$i});
 }
-die 'missing config file' unless (keys %cfg);
+die 'missing config file' unless (keys %$cfg);
 
 die 'missing some config parameters should be defined (irc_server, fb_app_id, fb_app_secret, fb_access_code, fb_page_id fb_postback_url)'
-  if (!defined $cfg{'irc_server'}
-      || !defined $cfg{'fb_app_id'}
-      || !defined $cfg{'fb_app_secret'}
-      || !defined $cfg{'fb_access_code'}
-      || !defined $cfg{'fb_page_id'}
-      || !defined $cfg{'fb_postback_url'}
-      || !defined $cfg{'db_user'}
-      || !defined $cfg{'db_pass'}
+  if (!defined $cfg->{'irc_server'}
+      || !defined $cfg->{'fb_app_id'}
+      || !defined $cfg->{'fb_app_secret'}
+      || !defined $cfg->{'fb_access_code'}
+      || !defined $cfg->{'fb_page_id'}
+      || !defined $cfg->{'fb_postback_url'}
+      || !defined $cfg->{'db_user'}
+      || !defined $cfg->{'db_pass'}
     );
-
-$cfg{irc_port} ||= 6667;
-$cfg{irc_channels} ||= ['#mubot4fb'];
-$cfg{irc_nick} ||= 'mubot4fb';
-$cfg{irc_name}||= $cfg{irc_nick};
-$cfg{irc_charset} ||= 'utf8';
-$cfg{database} ||= 'mubot4fb';
-
-my $bot = Mubot4FB->new(server => $cfg{'irc_server'},
-                       port => $cfg{'irc_port'},
-                       channels => $cfg{'irc_channels'},
-                       nick => $cfg{'irc_nick'},
-                       username => $cfg{'irc_name'},
-                       name => $cfg{'irc_name'},
-                       charset => $cfg{'irc_charset'},
-                       cfg => \%cfg)->run();
+$cfg = Data::Recursive::Encode->decode('utf8', $cfg);
+
+$cfg->{irc_port} ||= 6667;
+$cfg->{irc_channels} ||= ['#mubot4fb'];
+$cfg->{irc_nick} ||= 'mubot4fb';
+$cfg->{irc_name}||= $cfg->{irc_nick};
+$cfg->{irc_charset} ||= 'utf8';
+$cfg->{database} ||= 'mubot4fb';
+
+my $bot = Mubot4FB->new(server => $cfg->{'irc_server'},
+                       port => $cfg->{'irc_port'},
+                       channels => $cfg->{'irc_channels'},
+                       nick => $cfg->{'irc_nick'},
+                       username => $cfg->{'irc_name'},
+                       name => $cfg->{'irc_name'},
+                       charset => $cfg->{'irc_charset'},
+                       cfg => $cfg)->run();
 
 1;