#!/bin/perl ################################################################## # Execute perl statements passed in via a CGI form # ################################################################## # This script uses the cgi-lib.pl CGI library # available at http://cgi-lib.berkeley.edu/ require 'cgi-lib.pl'; ############ MAIN ############### # Print HTTP Header print &PrintHeader; # Point STDERR to STDOUT open (STDERR, ">&STDOUT"); # Flush STDERR and STDOUT select STDERR; $| = 1; select STDOUT; $| = 1; # Parse CGI arguments &ReadParse(*in); printTop(); evaluate($in{'code'}); printFoot(); exit 0; ############ SUBS ############### # converts any HTML special characters in the # code to the equivelant HTML escape characters # also adds line numbers to the code so that you can see # what's going on sub HTMLize { my $code = shift; # conversion $code =~ s//>/g; # add line numbers # make sure we have a \r\n at the end of the code $code .= "\r\n"; $code =~ s(.*\r\n)("
  • $&")ge; return $code; } # Print the top portion of the HTML page sub printTop { my $code = HTMLize($in{'code'}); print <<"END_OF_HTML" WebQA TNT: Perl 101 - PerlRun

    WebQA TNT: Perl 101 - PerlRun


    Evaluating code:
      $code
    </code>
    Results of the evaluation are:

    END_OF_HTML
    }
    
    # print the bottom portion of the html code
    sub printFoot {
    	print <<'END_OF_HTML'
    

    END_OF_HTML } # use an eval statement to actually execute the # statement passed in sub evaluate { package WorkSpace; $_ = shift; eval($_); print "Unrecognized command or syntax error.
    $@

    \n" if $@; print "
    \n"; package main; }