#!/usr/bin/perl
use strict;
use CGI;
use CGI::Carp 'fatalsToBrowser';

# instantiate a CGI object
my $cgi = new CGI;

# start some basic HTML output
print $cgi->header,
      $cgi->start_html;

my $q= $cgi->param('q');
#if ($q=~/^((?!-)[A-Za-z0-9-]{1, 63}(?<!-)\.)+[A-Za-z]{2, 6}$/){
if ($q=~/^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/){
  my $command= "/bin/whois $q";
  my ($status,  $output) = executeCommand($command);
  print "<pre>$output<\pre>";
}else{
  print"<pre>[$q]<\pre>";
}

print $cgi->end_html;




###############################################################################
# Usage : executeCommand (command) 
#Purpose : executes a command and returns output and status 
#Returns : exec status, std out 
#Parameters: command 
#Comments : my ($status,  $output) = executeCommand($command)
#              returns 0,null or 1,errorstring
################################################################################
sub executeCommand {
  my $command = join ' ', @_;
  reverse ($_ = qx{$command 2>&1}, $? >> 8);
}
