HOUDINI.pm
From Odwiki
This file is related to the PerlToHoudini pages. Place this file in your PERL libarary path or use the "use lib ..." syntax to point to the directory. Please feel free to correct any errors in this program. Cut and paste this text to your own file.
HOUDINI.pm
package HOUDINI;
use IO::Socket;
=head1 NAME
HOUDINI.pm - A class for I/O with Houdini via a socket connection
=head1 SYNOPSIS
my $houdini = new HOUDINI( [optional_port_number] );
=head1 DESCRIPTION
This class will connect to a socket opened by Houdini and send Hscript command and receive the output
from commands.
=cut
=item B<HOUDINI::new([optional_port_number)>
The constructor. This will call connect_to_houdini with an optional port number to establish the
connection with Houdini.
=cut
sub new
{
my $classname = shift;
my $port = shift;
my $self = { };
bless ($self, $classname);
$self->{connected} = 0;
$self->connect_to_houdini($port);
return $self;
}
=item B<bool HOUDINI::connect_to_houdini( [integer optional_port_number] )>
This will attempt to establish a connection with Houdini via the port numbers specified in the parameters,
or it will attempt to use the environment variable called OPEN_PORT. If there is no variable called OPEN_PORT,
it will try to use an arbitrary default port number. This is called by the contructor and so you may never
need to call it yourself.
=cut
sub connect_to_houdini()
{
my $self = shift;
my $port = shift || $ENV{OPEN_PORT} || 19191; #alternatively, use a default port here.
$self->{socket} = new IO::Socket::INET (PeerAddr => 'localhost',
PeerPort => $port,
Proto => 'tcp',
);
if( ! $self->{socket} )
{
print STDERR "Could not connect to Houdini on port $port. This port has not been opened from Houdini using the 'openport' command.\n";
$retval = 0;
}
$self->{connected} = defined($self->{socket});
return( $self->{connected} );
}
=item B<string HOUDINI::hcommand( string hscript_command [, FILEHANDLE socket] )>
This will send the command to the open port. It will use the socket stored within this object unless another
socket connection is supplied. The output of the hscript operation will be returned.
=cut
sub hcommand()
{
my $self = shift;
my $command = shift;
my $socket = shift || $self->{socket};
$self->{buffer}=""; #clear the result buffer
if( ! $socket )
{
print STDERR "Socket to Houdini is not open - cannot send command to Houdini.\n";
return("");
}
#send the command to houdini
print $socket "$command\n";
#receive data from houdini until the null terminator 0x00
my $chunk;
while( $chunk !~ /\x00/ )
{
$bytes_read = sysread ($socket, $chunk, 8192);
$self->{buffer} .= $chunk;
}
return( $self->{buffer} );
}
=item string B<HOUDINI::destroy()>
Class destructor will close the socket connection if it exists.
=cut
sub destroy()
{
my $self = shift;
if( $self->{socket} )
{
close( $self->{socket} );
}
}
1;



