title: Perl CGI produces unexpected output
body: <p>I have a Perl CGI script for online concordance application that searches for an instance of word in a text and prints the sorted output.</p>
  <pre><code>#!/usr/bin/perl -wT
  
  # middle.pl - a simple concordance
  
  
  
  # require
  use strict;
  use diagnostics;
  use CGI;
  
  
  # ensure all fatals go to browser during debugging and set-up
  # comment this BEGIN block out on production code for security
  BEGIN {
      $|=1;
      print &quot;Content-type: text/html\n\n&quot;;
      use CGI::Carp('fatalsToBrowser');
  }
  
  # sanity check
  my $q = new CGI;
  my $target = $q-&gt;param(&quot;keyword&quot;);
  my $radius = $q-&gt;param(&quot;span&quot;);
  my $ordinal = $q-&gt;param(&quot;ord&quot;);
  my $width = 2*$radius;
  my $file    = 'concordanceText.txt';
  if ( ! $file or ! $target ) {
  
      print &quot;Usage: $0 &lt;file&gt; &lt;target&gt;\n&quot;;
      exit;
      
  }
  
  # initialize
  my $count   = 0;
  my @lines   = ();
  $/          = &quot;&quot;; # Paragraph read mode
  
  # open the file, and process each line in it
  open(FILE, &quot; &lt; $file&quot;) or die(&quot;Can not open $file ($!).\n&quot;);
  while(&lt;FILE&gt;){
  
      # re-initialize
      my $extract = '';
      
      # normalize the data
      chomp;
      s/\n/ /g;        # Replace new lines with spaces
      s/\b--\b/ -- /g; # Add spaces around dashes
  
      # process each item if the target is found
      while ( $_ =~ /\b$target\b/gi ){
                  
          # find start position
          my $match = $1;
          my $pos   = pos;
          my $start = $pos - $radius - length($match);
  
          # extract the snippets
          if ($start &lt; 0){
              $extract = substr($_, 0, $width+$start+length($match));
              $extract = (&quot; &quot; x -$start) . $extract;
          }else{
              $extract = substr($_, $start, $width+length($match));
              my $deficit = $width+length($match) - length($extract);
              if ($deficit &gt; 0) {
                  $extract .= (&quot; &quot; x $deficit);
              }
      
          }
  
          # add the extracted text to the list of lines, and increment
          $lines[$count] = $extract;
          ++$count;
          
      }
      
  }
  
  sub removePunctuation {
      my $string = $_[0];
      $string = lc($string); # Convert to lowercase
      $string =~ s/[^-a-z ]//g; # Remove non-aplhabetic characters 
      $string =~ s/--+/ /g; #Remove 2+ hyphens with a space 
      $string =~s/-//g; # Remove hyphens
      $string =~ s/\s=/ /g;
      return($string);
      
  }
  
  sub onLeft {
      #USAGE: $word = onLeft($string, $radius, $ordinal);
      my $left = substr($_[0], 0, $_[1]);
      $left = removePunctuation($left);
      my @word = split(/\s+/, $left);
      return($word[-$_[2]]);
  }
  
  sub byLeftWords {
      my $left_a = onLeft($a, $radius, $ordinal);
      my $left_b = onLeft($b, $radius, $ordinal);
      lc($left_a) cmp lc($left_b);
  }
  
  
  # process each line in the list of lines
  
  print &quot;Content-type: text/plain\n\n&quot;;
  my $line_number = 0;
  foreach my $x (sort byLeftWords @lines){
      ++$line_number;
      printf &quot;%5d&quot;,$line_number;
      print &quot; $x\n\n&quot;;
  }
  
  # done
  exit;
  </code></pre>
  <p>The perl script produces expected result in terminal (command line). But the CGI script for online application produces unexpected output. I cannot figure out what mistake I am making in the CGI script. The CGI script should ideally produce the same output as the command line script. Any suggestion would be very helpful.</p>
  <h1>Command Line Output</h1>
  <p><a href="https://i.stack.imgur.com/i5pYs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/i5pYs.png" alt="Command Line Output" /></a></p>
  <h1>CGI Output</h1>
  <p><a href="https://i.stack.imgur.com/c5qBb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/c5qBb.png" alt="CGI Output" /></a></p>