split tie scalar into package ties
authorDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 9 Sep 2009 10:48:59 +0000 (10:48 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 9 Sep 2009 10:48:59 +0000 (10:48 +0000)
(which should be called kravata in Croatian :-)

lib/PXElator/server.pm
lib/PXElator/t/ties.t [new file with mode: 0755]
lib/PXElator/ties.pm [new file with mode: 0644]

index 84eab14..9a6b37f 100644 (file)
@@ -5,13 +5,15 @@ use strict;
 
 our $base_dir = '/srv/pxelator';
 
-tie our $ip,      'server::tie', 'server_ip' => '172.16.10.1';
-tie our $netmask, 'server::tie', 'natmask'   => '255.255.255.0';
-tie our $bcast,   'server::tie', 'bcast'     => '172.16.10.254';
-tie our $ip_from, 'server::tie', 'ip_from'   => 10;
-tie our $ip_to,   'server::tie', 'ip_to'     => 100;
-tie our $domain,  'server::tie', 'domain'    => 'pxelator.lan';
-tie our $new_clients, 'server::tie', 'new_clients' => $ip_to - $ip_from;
+use ties;
+
+tie our $ip,      'ties', 'server_ip' => '172.16.10.1';
+tie our $netmask, 'ties', 'natmask'   => '255.255.255.0';
+tie our $bcast,   'ties', 'bcast'     => '172.16.10.254';
+tie our $ip_from, 'ties', 'ip_from'   => 10;
+tie our $ip_to,   'ties', 'ip_to'     => 100;
+tie our $domain,  'ties', 'domain'    => 'pxelator.lan';
+tie our $new_clients, 'ties', 'new_clients' => $ip_to - $ip_from;
 
 warn "DEV $ip $bcast $netmask";
 
@@ -56,42 +58,4 @@ sub debug { shared('debug', @_) || 0 }
 
 warn "loaded";
 
-
-package server::tie;
-
-use File::Slurp;
-use Data::Dump qw/dump/;
-
-use server;
-
-sub TIESCALAR {
-       warn dump @_;
-       my ($class,$name,$default) = @_;
-
-       my $path = $server::base_dir . '/conf/' . $name;
-
-       my $o = {
-               path => $path,
-       };
-       write_file $o->{path}, $default unless -f $o->{path};
-
-warn "TIESCALAR $name ", $o->{path}, " [$default]";
-
-       bless \$o,$class;
-}
-
-sub STORE {
-       warn dump @_;
-       my ( $self, $value ) = @_;
-       write_file $$self->{path}, $value;
-}
-
-sub FETCH {
-       warn dump @_;
-       my $self = shift;
-       my $v = read_file $$self->{path};
-       chomp($v);
-       $v;
-}
-
-3;
+1;
diff --git a/lib/PXElator/t/ties.t b/lib/PXElator/t/ties.t
new file mode 100755 (executable)
index 0000000..62ae5cf
--- /dev/null
@@ -0,0 +1,18 @@
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+use autodie;
+
+use Test::More tests => 5;
+
+use_ok 'ties';
+
+tie my $test, 'ties', 'test', 42;
+ok( $test, 'tie' );
+cmp_ok( $test, '==', 42, 'get' );
+
+ok( $test = 'foobar', 'set' );
+cmp_ok( $test, 'eq', 'foobar', 'get' );
+
+unlink "/srv/pxelator/conf/test";
diff --git a/lib/PXElator/ties.pm b/lib/PXElator/ties.pm
new file mode 100644 (file)
index 0000000..09a8b05
--- /dev/null
@@ -0,0 +1,39 @@
+package ties;
+
+use warnings;
+use strict;
+
+use File::Slurp;
+use Data::Dump qw/dump/;
+
+#use server;
+
+sub TIESCALAR {
+       my ($class,$name,$default) = @_;
+
+#      my $path = $server::base_dir . '/conf/' . $name; # FIXME we can't call server because it require this package
+       my $path = "/srv/pxelator/conf/$name";
+
+       my $o = {
+               path => $path,
+       };
+       write_file $o->{path}, $default unless -f $o->{path};
+
+       warn "# TIESCALAR $name ", $o->{path}, " [$default]";
+
+       bless \$o,$class;
+}
+
+sub STORE {
+       my ( $self, $value ) = @_;
+       write_file $$self->{path}, $value;
+}
+
+sub FETCH {
+       my $self = shift;
+       my $v = read_file $$self->{path};
+       chomp($v);
+       $v;
+}
+
+1;