lib/Quancom.pm
changeset 12 ad264ee5d5ba
parent 10 d32454497beb
child 13 d6f681329542
--- a/lib/Quancom.pm	Thu Dec 25 11:23:35 2008 +0100
+++ b/lib/Quancom.pm	Sun Jan 04 18:42:46 2009 +0100
@@ -21,7 +21,8 @@
 use strict;
 use warnings;
 use Carp;
-use IO::Socket::INET;
+use IO::Socket::INET;    # FIXME: shold be loaded conditionally
+use IO::Socket::UNIX;    # FIXME: shold be loaded conditionally
 
 use Quancom::Result;
 
@@ -34,13 +35,21 @@
     my $self = bless {} => $class;
 
     $self->{peer} = shift or croak "need a peer address!";
-    $self->{peer} .= ":$DEFAULT_PORT"
-      unless $self->{peer} =~ /:\d+$/;
+
+    if ($self->{peer} !~ /\//) {
+        $self->{peer} .= ":$DEFAULT_PORT"
+          unless $self->{peer} =~ /:\d+$/;
 
-    $self->{socket} = new IO::Socket::INET(
-        Proto    => "tcp",
-        PeerAddr => $self->{peer}
-    );
+        $self->{socket} = new IO::Socket::INET(
+            Proto    => "tcp",
+            PeerAddr => $self->{peer}
+        );
+    }
+    else {
+        $self->{socket} = new IO::Socket::UNIX(Peer => $self->{peer});
+    }
+
+    $self->{socket} or croak "Can't create socket to $self->{peer}: $!\n";
 
     $self->{job} = 0;
     $self->{ok}  = undef;
@@ -60,6 +69,23 @@
     return $self->{last_result};
 }
 
+sub TIESCALAR {
+    my $class = shift;
+    my ($ip)  = @_;
+    my $self  = bless {} => $class;
+    warn "tied to ip $ip\n";
+
+    return $self;
+}
+
+sub STORE {
+    my $self = shift;
+    my ($key, $value) = @_;
+
+    #croak "invalid value \"$value\" (should be 0 or 1)\n";
+    warn "Set $key to $value\n";
+}
+
 sub _tx {
     my $self = shift;
     my $cmd  = shift;
@@ -68,7 +94,7 @@
     $cmd = "\x02" . sprintf("%02x", $self->{job}) . $cmd;   # add STX and job id
     $cmd .= sprintf("%02x", unpack("%8C*", $cmd));          # add checksum
 
-    warn "sending $cmd | " . unpack("H*", $cmd) . "\n";
+    warn "sending $cmd\n";
     $self->{socket}->print($cmd . "\r");
 }
 
@@ -91,7 +117,7 @@
 
     use Quancom;
 
-    my $quancom = new Quancom 172.16.0.22;
+    my $quancom = new Quancom "172.16.0.22";
     my $result  = $q->cmd("xxxxxx");
     if ($result->error) { die $result->error_message } 
     else { print $result->data }
@@ -101,10 +127,12 @@
 
 =over
 
-=item constructor B<new>( I<ip> )
+=item constructor B<new>( I<ip or socket name> )
 
 This method returns a new Quancom object if the connection was
-successfully established.
+successfully established. For testing you may use "0.0.0.0" as address,
+this disables the socket communication and just simulates the Quancom
+module.
 
 =item B<cmd>( I<string> )
 
@@ -126,7 +154,7 @@
 =head1 MORE EXAMPLES
 
     use Quancom;
-    my $quancom = new Quancom(172.20.20.1);
+    my $quancom = new Quancom("172.20.20.1");
     die "Sorry" if $quancom->cmd("xxxx")->error;
 
 =head1 SEE ALSO