
# Make sure we can do sockets

use IO::Socket ();
use Data::Dumper ();

# Some local lexicals

my $text;

# Satisy -require-

1;

#------------------------------------------------------------------------
# anyport
#
# Return a free port for listening
#
#  IN: 1 servername or IP address (defaults to "localhost")
# OUT: 1 random port number

sub anyport {

# Obtain the object
# Initialize the port
# If we can create a socket on a random port
#  Obtain the port (socket will be freed when if goes out of scope)

  my $self = shift;
  my $port = '';
  if (my $socket = IO::Socket::INET->new(
   Listen    => 5,
   LocalAddr => (shift || 'localhost'),
  )) {
    $port = $socket->sockport;
  }

# Make sure the system's freed up the port
# Return it

  sleep( 1 );
  return $port;
} #anyport

#------------------------------------------------------------------------
# ft
#
# Helper sub for doing tests inside a forked process
#
# a. called without parameter in void context: initialize
# b. called with parameter in void context: add test result + comment
# c. called without parameter in scalar context: return result

sub ft {

# If we're completed
#  Return what we got
# Elseif we're getting a test
#  Add result of test
# Else (we're initializing)
#  Initialize text

    if (defined wantarray) {
        return $text;
    } elsif (@_) {
        $text .= "$_[0]#$_[1]\n";
    } else {
        $text = '';
    }
} #ft

#------------------------------------------------------------------------
# pft
#
# Process forked test results
#
#  IN: 1 filename to read

sub pft {

# Obtain handle for reading file
# Process all lines

    open my $handle,$_[0] or die;
    chomp,&ok( split "#" ) while <$handle>;
} #pft

#------------------------------------------------------------------------
# slurp
#
# Slurp the contents of a file
#
#  IN: 1 filename
# OUT: 1 contents of file

sub slurp { open my $handle,$_[0]; local $/; <$handle> } #slurp

#------------------------------------------------------------------------
# splat
#
# Splat contents to a file
#
#  IN: 1 filename
#      2 contents of file

sub splat { open my $handle,">$_[0]"; print $handle $_[1] } #splat
