1 package Net
::REPL
::Base
;
9 Net::REPL::Base - Utilities for REPL clients and servers.
13 This contains the protocol implementation and various other simple
16 The REPL protocol uses length/value strings. A value is encoded to one
17 of these strings by concatenating its 32-bit unsigned big-endian
18 length with the value as a bytestring.
29 Read and return one length/value encoded string from the file handle.
31 Dies if an error (like the socket closing unexpectedly) occurs during
32 the read call. Returns a partial string or undef if the read is merely
40 my $total_length = unpack('N', $self->_read_fh(4));
41 if (not defined($total_length)) {
44 # $self->debug('Incoming message:', $total_length);
45 my $message = $self->_read_fh($total_length);
46 return unpack('a*', $message);
50 my ($self, $length) = @_;
53 my $result = sysread($self->{'fh'}, $data, $length);
54 if (not defined($result)) {
55 $self->debug("REPL PID $$ reading: $!")
61 =head3 lv_send($message)
63 Encode $message as a length/value string and write it to the file handle.
68 my ($self, $message) = @_;
70 my $length = length($message);
71 my $initial = pack('N', $length);
72 $self->{'fh'}->send($initial . $message);
73 $self->{'fh'}->flush();
78 Write words to the debug log according to the current debug level.
80 The default implementation writes to STDERR and uses the debug level
81 as a boolean (i.e., 0 or other false values disable it).
86 my ($self, @words) = @_;
88 if ($self->{'debug'}) {
89 print STDERR
join(' ', 'REPL:', @words) . "\n";
93 =head3 formatted_eval($code)
95 Eval the Perl $code and return the Dumper'd or serialized result.
99 # TODO: integrate with Devel::REPL
101 my ($self, $input) = @_;
105 local $SIG{'__DIE__'} = 'DEFAULT';
106 @output = eval('sub { ' . $input . '}->();');
108 # TODO: should this also pass through warnings?
113 my $dumper = Data
::Dumper
->new(\
@output);
115 return $dumper->Dump();
122 my ($self, %socket_args) = @_;
124 my $domain = $socket_args{'Domain'};
125 if (not defined($domain)) {
126 die('Socket "Domain" argument is mandatory');
129 my $pkg = $IO::Socket
::domain2pkg
[$domain];
133 return IO
::Socket
->new(