move shared (between processes) configuration files into own package
authorDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 31 Jul 2009 13:16:11 +0000 (13:16 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 31 Jul 2009 13:16:11 +0000 (13:16 +0000)
lib/PXElator/config.pm [new file with mode: 0644]
lib/PXElator/server.pm
lib/PXElator/t/config.t [new file with mode: 0755]

diff --git a/lib/PXElator/config.pm b/lib/PXElator/config.pm
new file mode 100644 (file)
index 0000000..0c42481
--- /dev/null
@@ -0,0 +1,21 @@
+package config;
+
+use warnings;
+use strict;
+
+use server;
+use File::Slurp;
+
+sub shared {
+       my ($name, $value) = @_;
+
+       my $path ="$server::base_dir/conf/$server::ip/$name";
+       if ( defined $value ) {
+               write_file $path, $value;
+       } else {
+               $value = read_file $path if -e $path;
+       }
+       return $value;
+}
+
+1;
index cd1c07b..f7600c6 100644 (file)
@@ -2,7 +2,6 @@ package server;
 
 use warnings;
 use strict;
-use File::Slurp;
 
 our $ip      = '172.16.10.1';
 our $netmask = '255.255.255.0';
@@ -11,16 +10,9 @@ our ( $ip_from, $ip_to ) = ( 10, 100 );
 
 our $base_dir = '/home/dpavlin/llin/pxelator';
 
+use config;
+
 our $debug = 0;
-sub debug {
-       my $new = shift;
-       my $path ="$base_dir/conf/debug";
-       if ( defined $new ) {
-               write_file $path, $debug = $new;
-       } else {
-               $debug = read_file $path if -e $path;
-       }
-       return $debug;
-}
+sub debug { $debug = config::shared('debug', @_) }
 
 warn "loaded";
diff --git a/lib/PXElator/t/config.t b/lib/PXElator/t/config.t
new file mode 100755 (executable)
index 0000000..3408c37
--- /dev/null
@@ -0,0 +1,15 @@
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+use autodie;
+
+use Test::More tests => 3;
+use Data::Dump qw/dump/;
+
+use_ok 'config';
+
+ok( my $test = config::shared( 'test', 42 ), 'set shared' );
+diag $test;
+cmp_ok( $test, '==', config::shared( 'test' ), 'get shared' );
+