Exercise Construction Steps (Cont.)

  1. Calling a CGI (Common Gateway Interface) Perl Script (Perl)
  2. Java instead of Perl programming is the focus of this demonstration, but Perl is popular for web programming. Therefore, the CGI Perl script SearchString.pl below is made minimal and performs the following three tasks:

    • The first part is to retrieve user’s data from the Web and put it into variables by using the param function. For example, after executing the following commands:
         use CGI; 
         $query  = new CGI;
         $string = $query->param( 'string' );
      the variable $string contains “PHP” from the web input:
         <input type="text" name="string" value="PHP">
      For details, check Perl CGI web-related module.

    • It then processes the HTML elements such as checkboxes and textarea. For details, check CGI Programming 101.

    • The third task does the actual processing by calling the Java program.

    ~/public_html/cgi-bin/280/5/SearchString.pl
    #!/usr/bin/perl
    use CGI; 
    $query  = new CGI;
    $act    = $query->param('act');
    $string = $query->param('string');
    $query1 = $query->param('query');
    
    if ( $act eq "Search String" ) {
      # Print HTML.
      print ( "Content-type: text/html\n\n" );
    
      # Use "here-doc" syntax.
      print <<EndofHTML;
      <html>
       <body>
        <table width="100%" height="80%">
         <tr>
          <td align="center">
           <font size="+0">
    EndofHTML
    
      # Remove leading and trailing spacing.
      $string =~ s/^\s*(\S*)\s*$/$1/;
      $query1 =~ s/^\s*(\S*)\s*$/$1/;
    
      # For security, remove some Unix metacharacters.
      $string =~ s/;|>|>>|<|\*|\?|\&|\|//g;
      $query1 =~ s/;|>|>>|<|\*|\?|\&|\|//g;
    
      # Compose a Java command.
      $cmd    =  "/usr/bin/java SearchString '" . $string . "' '" . $query1 . "'";
      print( $cmd );    system( $cmd );
    
      print <<EndofHTML;
            <form> 
             <input type="button" value=" Back " onclick="history.go(-1);return false;" />
            </form>
           </font>
          </td>
         </tr>
        </table>
       </body>
      </html>
    EndofHTML
    }
    elsif ( $act eq "HTML source" ) {
      print ( "Content-type: text/plain\n\n" );
      $cmd  = "/usr/bin/lynx -dump -source " . $ENV{HTTP_REFERER}; 
      $cmd .= "; echo \n\n\n\n";
      system( $cmd );
    }
    elsif ( $act eq "Perl source" ) {
      print ( "Content-type: text/plain\n\n" );
      system( "/bin/cat SearchString.pl; echo \n\n\n\n" );
    }
    elsif ( $act eq "Java source" ) {
      print ( "Content-type: text/plain\n\n" );
      system( "/bin/cat SearchString.java; echo \n\n\n\n" );
    }
    elsif ( $act eq "Help" ) {
      print ( "Content-type: text/html\n\n" );
      system( "/bin/cat  Help.html" );
    }
    else {
      print( "Content-type: text/html\n\n" );
      print( "No such option: <em>$act</em>" );
    }



      Actions speak louder than words.