Updates from work
[clearscm.git] / lib / CmdLine.pm
index 40ed702..6920f0b 100644 (file)
@@ -93,13 +93,19 @@ use Display;
 use Utils;
 
 use Term::ReadLine;
-use Term::ANSIColor qw (color);
+use Term::ANSIColor qw(color);
 
 # Package globals
 my $_pos = 0;
 my $_haveGnu;
 
-my (%_cmds, $_cmdline, $_attribs);
+my $promptColor = color('bold yellow');
+my $inputColor  = color('underline');
+my $resetColor  = color('reset');
+
+my (%_cmds, $_attribs);
+
+our $_cmdline;
 
 BEGIN {
   # See if we can load Term::ReadLine::Gnu
@@ -114,7 +120,9 @@ BEGIN {
 } # BEGIN
 
 # Share %opts
-our %opts;
+our %opts = (
+  color => 1,
+);
 
 my %builtin_cmds = (
   history       => {
@@ -146,8 +154,8 @@ the specified file.',
     description => 'Sets a variable. Note that expression can be any valid expression.',
   },
 
-  vars         => {
-    help       => 'vars',
+  vars          => {
+    help        => 'vars',
     description => 'Displays all known variables.',
   },
 
@@ -167,7 +175,7 @@ the specified file.',
   },
 );
 
-sub _cmdCompletion ($$) {
+sub _cmdCompletion($$) {
   my ($text, $state) = @_;
 
   return unless %_cmds;
@@ -182,17 +190,17 @@ sub _cmdCompletion ($$) {
   } # for
 
   return;
-}# _cmdCompletion
+} # _cmdCompletion
 
-sub _complete ($$$$) {
+sub _complete($$$$) {
   my ($text, $line, $start, $end) = @_;
 
   return $_cmdline->completion_matches ($text, \&CmdLine::_cmdCompletion);
 } # _complete
 
-sub _gethelp () {
+sub _gethelp() {
   my ($self) = @_;
-  
+
   return unless %_cmds;
 
   my $line = $_cmdline->{line_buffer};
@@ -205,10 +213,10 @@ sub _gethelp () {
 
   # Sometimes we are called by ReadLine's callback and can't pass $self
   if (ref $self eq 'CmdLine') {
-    $self->help ($line);
+    $self->help($line);
   } else {
-    $CmdLine::cmdline->help ($line);
-  } # if  
+    $CmdLine::cmdline->help($line);
+  } # if
 
   $_cmdline->on_new_line;
 } # _gethelp
@@ -217,21 +225,20 @@ sub _interpolate ($) {
   my ($self, $str) = @_;
 
   # Skip interpolation for the perl command (Note this is raid specific)
-  return $str
-    if $str =~ /^\s*perl\s*/i;
+  return $str if $str =~ /^\s*perl\s*/i;
 
   while ($str =~ /\$/) {
     if ($str =~ /\$(\w+)/) {
       my $varname = $1;
 
-      if (defined $self->{vars}{$varname}) {
-       if ($self->{vars}{$varname} =~ / /) {
-         $str =~ s/\$$varname/\'$self->{vars}{$varname}\'/;
-       } else {
+      if ($self->{vars}{$varname}) {
+        if ($self->{vars}{$varname} =~ / /) {
+          $str =~ s/\$$varname/\'$self->{vars}{$varname}\'/;
+        } else {
           $str =~ s/\$$varname/$self->{vars}{$varname}/;
-       } # if
+        } # if
       } else {
-       $str =~ s/\$$varname//;
+        $str =~ s/\$$varname//;
       } # if
     } # if
   } # while
@@ -239,7 +246,7 @@ sub _interpolate ($) {
   return $str;
 } # _interpolate
 
-sub _builtinCmds ($) {
+sub _builtinCmds($) {
   my ($self, $line) = @_;
 
   unless (defined $line) {
@@ -259,7 +266,7 @@ sub _builtinCmds ($) {
       system $1;
     } # if
 
-    #$_cmdline->remove_history ($_cmdline->where_history);
+    #$_cmdline->remove_history($_cmdline->where_history);
 
     return;
   } # if
@@ -268,15 +275,14 @@ sub _builtinCmds ($) {
     $cmd = $1;
   } # if
 
-  return
-    unless $cmd;
+  return unless $cmd;
 
   my @parms;
 
   # Search for matches of partial commands
   my $foundCmd;
 
-  foreach (keys %builtin_cmds) {    
+  for (keys %builtin_cmds) {
     if ($_ eq $cmd) {
       # Exact match - honor it
       $foundCmd = $cmd;
@@ -292,7 +298,7 @@ sub _builtinCmds ($) {
         last;
       } # unless
     } # if
-  } # foreach
+  } # for
 
   # If we found a command, substitute it into line
   if ($foundCmd) {
@@ -303,7 +309,7 @@ sub _builtinCmds ($) {
   if ($builtin_cmds{$cmd}) {
     if ($line =~ /^\s*help\s*(.*)/i) {
       if ($1 =~ /(.+)$/) {
-        $self->help ($1);
+        $self->help($1);
       } else {
         $self->help;
       } # if
@@ -311,22 +317,22 @@ sub _builtinCmds ($) {
       if ($1 =~ /(\d+)\s+(\d+)\s*$/) {
         $self->history ('list', $1, $2);
       } elsif ($1 =~ /^\s*$/) {
-        $self->history ('list');
+        $self->history('list');
       } else {
         error "Invalid usage";
-        $self->help ('history');
+        $self->help('history');
       } # if
     } elsif ($line =~ /^\s*savehist\s*(.*)/i) {
       if ($1 =~ /(\S+)\s+(\d+)\s+(\d+)\s*$/) {
-        $self->history ('save', $1, $2, $3);
+        $self->history('save', $1, $2, $3);
       } else {
         error 'Invalid usage';
-        $self->help ('savehist');
+        $self->help('savehist');
       } # if
     } elsif ($line =~ /^\s*get\s*(.*)/i) {
       if ($1 =~ (/^\$*(\S+)\s*$/)) {
-        my $value = $self->_get ($1);
-        
+        my $value = $self->_get($1);
+
         if ($value) {
           display "$1 = $value";
         } else {
@@ -334,14 +340,14 @@ sub _builtinCmds ($) {
         } # if
       } else {
         error 'Invalid usage';
-        $self->help ('get');
+        $self->help('get');
       } # if
     } elsif ($line =~ /^\s*set\s*(.*)/i) {
       if ($1 =~ /^\$*(\S+)\s*=\s*(.*)/) {
-        $self->_set ($1, $2)
+        $self->_set($1, $2)
       } else {
         error 'Invalid usage';
-        $self->help ('set');
+        $self->help('set');
       } # if
     } elsif ($line =~ /^\s*source\s+(\S+)/i) {
       $result = $self->source ($1);
@@ -350,11 +356,12 @@ sub _builtinCmds ($) {
     } elsif ($line =~ /^\s*color\s*(.*)/i) {
       if ($1 =~ /(1|on)/i) {
         $opts{color} = 1;
-        delete $ENV{ANSI_COLORS_DISABLED}
-          if $ENV{ANSI_COLORS_DISABLED};
+        delete $ENV{ANSI_COLORS_DISABLED} if $ENV{ANSI_COLORS_DISABLED};
+        $self->set_prompt;
       } elsif ($1 =~ /(0|off)/i) {
-        $opts{trace} = 0;
+        $opts{color} = 0;
         $ENV{ANSI_COLORS_DISABLED} = 1;
+        $self->set_prompt;
       } elsif ($1 =~ /\s*$/) {
         if ($ENV{ANSI_COLORS_DISABLED}) {
           display 'Color is currently off';
@@ -363,7 +370,7 @@ sub _builtinCmds ($) {
         } # if
       } else {
         error 'Invalid usage';
-        $self->help ('color');
+        $self->help('color');
       } # if
     } elsif ($line =~ /^\s*trace\s*(.*)/i) {
       if ($1 =~ /(1|on)/i) {
@@ -378,7 +385,7 @@ sub _builtinCmds ($) {
         } # if
       } else {
         error 'Invalid usage';
-        $self->help ('trace');
+        $self->help('trace');
       } # if
     } # if
   } # if
@@ -388,11 +395,11 @@ sub _builtinCmds ($) {
 
 sub _interrupt () {
   # Announce that we have hit an interrupt
-  print color ('yellow') . "<Control-C>\n" . color ('reset');
+  print color('yellow') . "<Control-C>\n" . color('reset');
 
   # Free up all of the line state info
   $_cmdline->free_line_state;
-  
+
   # Allow readline to clean up
   $_cmdline->cleanup_after_signal;
 
@@ -400,50 +407,50 @@ sub _interrupt () {
   $_cmdline->on_new_line;
   $_cmdline->{line_buffer} = '';
   $_cmdline->redisplay;
-  
+
   return;
 } # _interrupt
 
-sub _displayMatches ($$$) {
+sub _displayMatches($$$) {
   my ($matches, $numMatches, $maxLength) = @_;
-  
+
   # Work on a copy... (Otherwise we were getting "Attempt to free unreferenced
   # scalar" internal errors from perl)
   my @Matches;
-  
-  push @Matches, $_ foreach (@$matches);  
+
+  push @Matches, $_ for (@$matches);
 
   my $match = shift @Matches;
-  
+
   if ($match =~/^\s*(.*) /) {
     $match = $1;
   } elsif ($match =~ /^\s*(\S+)$/) {
     $match = '';
   } # if
-  
+
   my %newMatches;
-  
-  foreach (@Matches) {
+
+  for (@Matches) {
     # Get next word
     s/^$match//;
-    
+
     if (/(\w+)/) {
       $newMatches{$1} = $1;
     } # if
-  } # foreach
-  
+  } # for
+
   my @newMatches = sort keys %newMatches;
 
   unshift @newMatches, $match;
-  
-  $_cmdline->display_match_list (\@newMatches);
+
+  $_cmdline->display_match_list(\@newMatches);
   $_cmdline->on_new_line;
   $_cmdline->redisplay;
-  
+
   return;
 } # _displayMatches
-  
-sub new (;$$%) {
+
+sub new(;$$%) {
   my ($class, $histfile, $eval, %cmds) = @_;
 
 =pod
@@ -507,11 +514,11 @@ Returns:
 
   my $me = get_me;
 
-  $histfile ||= ".${me}_hist";
-  
+  $histfile ||= "$ENV{HOME}/.${me}_hist";
+
   error "Creating bogus .${me}_hist file!"
-    if $me eq '-';
-    
+    if $me eq '-' or $me eq '';
+
   unless (-f $histfile) {
     open my $hist, '>', $histfile
       or error "Unable to open $histfile for writing - $!", 1;
@@ -520,7 +527,7 @@ Returns:
   } # unless
 
   # Instantiate a commandline
-  $_cmdline = Term::ReadLine->new ($me);
+  $_cmdline = Term::ReadLine->new($me);
 
   # Store the function pointer of what to call when sourcing a file or
   # evaluating an expression.
@@ -532,17 +539,21 @@ Returns:
     } # if
   } # if
 
+  $self->{promptColor} = $promptColor;
+  $self->{inputColor}  = $inputColor;
+  $self->{resetColor}  = $resetColor;
+
   # Default prompt is "$me:"
-  $self->{prompt} = "$me:";
+  $self->set_prompt("$me:");
 
   # Set commands
-  $self->set_cmds (%cmds);
+  $self->set_cmds(%cmds);
 
   # Set some ornamentation
-  $_cmdline->ornaments ('s,e,u,') unless $Config{cppflags} =~ /win32/i;
+  $_cmdline->ornaments('e,,u') unless $Config{cppflags} =~ /win32/i;
 
   # Read in history
-  $self->set_histfile ($histfile);
+  $self->set_histfile($histfile);
 
   # Generator function for completion matches
   $_attribs = $_cmdline->Attribs;
@@ -555,7 +566,7 @@ Returns:
   # The following functionality requires Term::ReadLine::Gnu
   if ($_haveGnu) {
     # Bind a key to display completion
-    $_cmdline->add_defun ('help-on-command', \&CmdLine::_gethelp, ord ("\cl"));
+    $_cmdline->add_defun('help-on-command', \&CmdLine::_gethelp, ord ("\cl"));
 
     # Save a handy copy of RL_PROMPT_[START|END]_IGNORE
     $self->{ignstart} = $_cmdline->RL_PROMPT_START_IGNORE;
@@ -566,11 +577,11 @@ Returns:
     $opts{trace} = 0;
     $ENV{ANSI_COLORS_DISABLED} = 1;
   } # if
-  
+
   return $self;
 } # new
 
-sub get () {
+sub get() {
   my ($self) = @_;
 
 =pod
@@ -612,9 +623,12 @@ Returns:
     my $prompt = $self->{prompt};
 
     $prompt =~ s/\\\#/$self->{cmdnbr}/g;
-    
+
+    # Now color it if color is on
+    $prompt = "$self->{resetColor}$self->{promptColor}$prompt$self->{resetColor}$self->{inputColor}" if $self->{promptColor};
+
     use POSIX;
-    
+
     # Term::ReadLine::Gnu restarts whatever system call it is using, such that
     # once we ctrl C, we don't get back to Perl until the user presses enter, 
     # finally whereupon we get our signal handler called. We use sigaction
@@ -622,45 +636,48 @@ Returns:
     # routine. Sure, sigaction poses race conditions, but you'd either be at a
     # prompt or executing whatever command your prompt prompted for. The user
     # has said "Abort that!" with his ctrl-C and we're attempting to honor that.
-    
+
     # Damn Windows can't do any of this
     my $oldaction;
-    
+
     if ($Config{cppflags} !~ /win32/i) {
       my $sigset    = POSIX::SigSet->new;
       my $sigaction = POSIX::SigAction->new (\&_interrupt, $sigset, 0);
-      
+
       $oldaction = POSIX::SigAction->new;
-    
+
       # Set up our unsafe signal handler
-      POSIX::sigaction (&POSIX::SIGINT, $sigaction, $oldaction);
+      POSIX::sigaction(&POSIX::SIGINT, $sigaction, $oldaction);
     } # if
 
-    $line = $_cmdline->readline ($prompt);
+    $line = $_cmdline->readline($prompt);
+
+    display_nolf $resetColor;
 
     # Restore the old signal handler
     if ($Config{cppflags} !~ /win32/i) {
-      POSIX::sigaction (&POSIX::SIGINT, $oldaction);
+      POSIX::sigaction(&POSIX::SIGINT, $oldaction);
     } # if
 
-    $line = $self->_interpolate ($line)
-      if $line;
+    $line = $self->_interpolate($line) if $line;
 
-    $self->{cmdnbr}++
-      unless $self->{sourcing};
+    $self->{cmdnbr}++ unless $self->{sourcing};
 
-    ($cmd, $line, $result) = $self->_builtinCmds ($line);
+    ($cmd, $line, $result) = $self->_builtinCmds($line);
 
-    $line = ''
-      unless $cmd;
+    $line = '' unless $cmd;
   } while ($cmd and $builtin_cmds{$cmd});
 
-  return ($line, $result);
+  if (wantarray) {
+    return ($line, $result);
+  } else {
+    return $result || $line;
+  } # if
 } # get
 
 sub set_cmds (%) {
   my ($self, %cmds) = @_;
-  
+
 =pod
 
 =head2 set_cmds
@@ -698,17 +715,17 @@ Returns:
   %_cmds = %cmds;
 
   # Add in builtins
-  foreach (keys %builtin_cmds) {
+  for (keys %builtin_cmds) {
     $_cmds{$_}{help}        = $builtin_cmds{$_}{help};
     $_cmds{$_}{description} = $builtin_cmds{$_}{description};
-  } # foreach
+  } # for
 
   return;
 } # set_cmds
 
 sub set_prompt ($) {
   my ($self, $prompt) = @_;
-  
+
 =pod
 
 =head2 set_prompt
@@ -743,14 +760,22 @@ Returns:
 
 =cut
 
-  my $return = $self->{prompt};
+  my $oldPrompt = $self->{prompt};
 
-  $self->{prompt} = $prompt;
+  $self->{prompt} = $prompt if $prompt;
 
-  return $return;
+  if ($opts{color}) {
+    $self->{promptColor} = $promptColor;
+    $self->{resetColor}  = $resetColor;
+  } else {
+    undef $self->{promptColor};
+    undef $self->{resetColor};
+  } # if
+
+  return $oldPrompt;
 } # set_prompt
 
-sub set_histfile ($) {
+sub set_histfile($) {
   my ($self, $histfile) = @_;
 
 =pod
@@ -789,15 +814,15 @@ Returns:
 
   if ($histfile and -f $histfile) {  
     $self->{histfile} = $histfile;
-    
+
     if ($_haveGnu) {
       # Clear old history (if any);
       $_cmdline->clear_history;
 
       # Now read histfile
-      $_cmdline->ReadHistory ($histfile);
+      $_cmdline->ReadHistory($histfile);
     } # if
-    
+
     # Determine the number of lines in the history file
     open my $hist, '<', $histfile;
 
@@ -811,7 +836,7 @@ Returns:
   return;
 } # set_histfile
 
-sub set_eval (;\&) {
+sub set_eval(;\&) {
   my ($self, $eval) = @_;
 
 =pod
@@ -856,7 +881,7 @@ Returns:
   return $returnEval;
 } # set_eval
 
-sub help (;$) {
+sub help(;$) {
   my ($self, $cmd) = @_;
 
 =pod
@@ -903,34 +928,34 @@ Returns:
   $cmd ||= '';
   $cmd =~ s/^\s+//;
   $cmd =~ s/\s+$//;
-  
+
   if ($cmd =~ /^\s*(.+)/) {
     my ($searchStr, $helpFound);
-    
+
     $searchStr = $1;
 
-    foreach (sort keys %_cmds) {
+    for (sort keys %_cmds) {
       if (/$searchStr/i) {
-             $helpFound = 1;
+        $helpFound = 1;
+
+        my $cmdcolor = $builtin_cmds{$_} ? color('cyan') : color ('magenta');
+        my $boldOn   = $builtin_cmds{$_} ? color('white on_cyan') : color ('white on_magenta');
+        my $boldOff  = color('reset') . $cmdcolor;
 
-        my $cmdcolor = $builtin_cmds{$_} ? color ('cyan') : color ('magenta');
-        my $boldOn   = $builtin_cmds{$_} ? color ('white on_cyan') : color ('white on_magenta');
-        my $boldOff  = color ('reset') . $cmdcolor;
-         
-        my $cmd  = "$cmdcolor$_";
+           $cmd  = "$cmdcolor$_";
            $cmd =~ s/($searchStr)/$boldOn$1$boldOff/g;
            $cmd .= " $_cmds{$_}{parms}"  if $_cmds{$_}{parms};
            $cmd .= color ('reset');
            $cmd .= " - $_cmds{$_}{help}" if $_cmds{$_}{help};
-        
+
         push @help, $cmd;
 
         if ($_cmds{$_}{description}) {
           push @help, "  $_"
-            foreach (split /\n/, $_cmds{$_}{description});
-             } # if
+            for (split /\n/, $_cmds{$_}{description});
+        } # if
       } # if
-    } # foreach
+    } # for
 
     unless ($helpFound) {
       display "I don't know about $cmd";
@@ -938,7 +963,7 @@ Returns:
       return;
     } # if
   } else {
-    foreach (sort keys %_cmds) {
+    for (sort keys %_cmds) {
       my $cmdcolor = $builtin_cmds{$_} ? color ('cyan') : color ('magenta');
 
       my $cmd  = "$cmdcolor$_";
@@ -949,18 +974,18 @@ Returns:
       push @help, $cmd;
 
       if ($_cmds{$_}{description}) {
-             push @help, "  $_"
-        foreach (split /\n/, $_cmds{$_}{description});
+        push @help, "  $_"
+        for (split /\n/, $_cmds{$_}{description});
       } # if
-    } # foreach
+    } # for
   } # if
 
-  $self->handleOutput ($cmd, @help);
+  $self->handleOutput($cmd, @help);
 
   return;
 } # help
 
-sub history (;$) {
+sub history(;$) {
   my ($self, $action) = @_;
 
 =pod
@@ -1012,7 +1037,7 @@ Returns:
 
     return;
   } # if
-  
+
   my ($file, $start, $end);
 
   if ($action eq 'list') {
@@ -1023,15 +1048,15 @@ Returns:
     $start = $_[3];
     $end   = $_[4];
   } elsif ($action eq 'redo') {
-    $_cmdline->remove_history ($_cmdline->where_history);
+    $_cmdline->remove_history($_cmdline->where_history);
 
     my $nbr  = $_[2];
-    my $line = $_cmdline->history_get ($nbr);
+    my $line = $_cmdline->history_get($nbr);
 
-    $_cmdline->add_history ($line);
+    $_cmdline->add_history($line);
     display $line;
 
-    my ($cmd, $result) = $self->_builtinCmds ($line);
+    my ($cmd, $result) = $self->_builtinCmds($line);
 
     if ($builtin_cmds{$cmd}) {
       return;
@@ -1064,26 +1089,26 @@ Returns:
 
     if ($action eq 'save') {
       unless ($file) {
-       error "Usage: savehist <file> [<start> <end>]";
-       return;
+        error "Usage: savehist <file> [<start> <end>]";
+        return;
       } # unless
 
       if (-f $file) {
-       display_nolf "Overwrite $file (yN)? ";
+        display_nolf "Overwrite $file (yN)? ";
 
-       my $response = <STDIN>;
+        my $response = <STDIN>;
 
-       unless ($response =~ /(y|yes)/i) {
-         display "Not overwritten";
-         return;
-       } # unless
+        unless ($response =~ /(y|yes)/i) {
+          display "Not overwritten";
+          return;
+        } # unless
       } # if
 
       my $success = open $savefile, '>', $file;
 
       unless ($success) {
-       error "Unable to open history file $file - $!";
-       return;
+        error "Unable to open history file $file - $!";
+        return;
       } # unless
     } # if
 
@@ -1093,9 +1118,9 @@ Returns:
       last unless $histline;
 
       if ($action eq 'list') {
-       display "$pos) $histline";
+        display "$pos) $histline";
       } else {
-       print $savefile "$histline\n";
+        print $savefile "$histline\n";
       } # if
     } # for
 
@@ -1106,12 +1131,12 @@ Returns:
   return;
 } # history
 
-sub _get ($$) {
+sub _get($$) {
   my ($self, $name) = @_;
 
 =pod
 
-=head2 _get ($name)
+=head2 _get($name)
 
 This method gets a variable to a value stored in the CmdLine
 object.
@@ -1147,7 +1172,7 @@ Returns:
   return $self->{vars}{$name}
 } # _get
 
-sub _set ($$) {
+sub _set($$) {
   my ($self, $name, $value) = @_;
 
 =pod
@@ -1191,15 +1216,15 @@ Returns:
 
   my $returnValue = $self->{vars}{$name};
 
-  if (defined $value) {
-    $value = $self->_interpolate ($value);
-    
+  if ($value) {
+    $value = $self->_interpolate($value);
+
     # Do not call eval if we are setting result - otherwise we recurse
     # infinitely.
     unless ($name eq 'result') {
       no strict;
-      $value = $self->{eval} ($value)
-       if $self->{eval};
+      $value = $self->{eval}($value)
+        if $self->{eval};
       use strict;
     } # unless
 
@@ -1211,12 +1236,12 @@ Returns:
   return $returnValue;
 } # _set
 
-sub vars ($) {
+sub vars($) {
   my ($self, $cmd) = @_;
 
 =pod
 
-=head2 vars ($name)
+=head2 vars($name)
 
 This method will print out all known variables
 
@@ -1247,14 +1272,14 @@ Returns:
 =cut
 
   my @output;
-  
+
   push @output, "$_ = $self->{vars}{$_}"
-    foreach (keys %{$self->{vars}});
-    
-  $self->handleOutput ($cmd, @output);
+    for (keys %{$self->{vars}});
+
+  $self->handleOutput($cmd, @output);
 } # vars
 
-sub handleOutput ($@) {
+sub handleOutput($@) {
   my ($self, $line, @output) = @_;
 
 =pod
@@ -1300,7 +1325,7 @@ Returns:
 =cut
 
   my ($outToFile, $pipeToCmd);
-  
+
   # Handle piping and redirection
   if ($line =~ /(.*)\>{2}\s*(.*)/) {
     $line      = $1;
@@ -1315,23 +1340,20 @@ Returns:
 
   # Store @output
   $self->{output} = \@output;
-  
+
   if ($pipeToCmd) {
     my $pipe;
-    
+
     local $SIG{PIPE} = 'IGNORE';
-    
-    open $pipe, "|$pipeToCmd"
-      or undef $pipe;
-    
+
+    open $pipe, '|', $pipeToCmd or undef $pipe;
+
     # TODO: Not handling the output here. Need open2 and then recursively call
     # handleOutput.
     if ($pipe) {
-      print $pipe "$_\n"
-        foreach (@output);
-        
-      close $pipe
-        or error "Unable to close pipe for $pipeToCmd - $!";
+      print $pipe "$_\n" for (@output);
+
+      close $pipe or error "Unable to close pipe for $pipeToCmd - $!";
     } else {
       error "Unable to open pipe for $pipeToCmd - $!";
     } # if
@@ -1339,25 +1361,24 @@ Returns:
     unless ($outToFile) {
       PageOutput @output;
     } else {
-      open my $output, ">$outToFile";
-      
+      open my $output, '>', $outToFile;
+
       if ($output) {
-        print $output "$_\n"
-          foreach (@output);
+        print $output "$_\n" for (@output);
 
         close $output;
-      
+
         undef $outToFile;
       } else {
         error "Unable to open $outToFile for writing - $!"
       } # if
     } # unless
   } # if
-  
+
   return;
 } # handleOutput
 
-sub source ($) {
+sub source($) {
   my ($self, $file) = @_;
 
 =pod
@@ -1428,25 +1449,25 @@ Returns:
     display "$prompt$_" if $CmdLine::opts{trace};
 
     next if /^\s*($|\#)/;
-    
+
     $_ = $self->_interpolate ($_);
-    
+
     # Check to see if it's a builtin
-    my ($cmd, $line, $result) = $self->_builtinCmds ($_);
-    
+    my ($cmd, $line, $result) = $self->_builtinCmds($_);
+
     next if $builtin_cmds{$cmd};
 
     no strict;
-    $result = $self->{eval} ($line);
+    $result = $self->{eval}($line);
     use strict;
-    
+
     if (defined $result) {
       if (ref \$result eq 'SCALAR') {
         PageOutput (split /\n/, $result);
       } else {
         display "Sorry but I cannot display structured results";
       } #  if
-    } # if    
+    } # if
   } # while
 
   $self->{sourcing} = 0;
@@ -1459,8 +1480,10 @@ Returns:
 sub DESTROY {
   my ($self) = @_;
 
-  $_cmdline->WriteHistory ($self->{histfile})
+  $_cmdline->WriteHistory($self->{histfile})
     if $_cmdline and $_haveGnu;
+
+  return;
 } # DESTROY
 
 our $cmdline = CmdLine->new;