Falkkin

Your perl tutor is great, just what I needed to teach a simple Perl 101 class. I had one additional requirement though, I wanted to allow anyone to do simple Perl evals without installing Perl or having a logon to a box with Perl installed. I also wanted to be able to demo simple Perl constructs real time for people without having to log into a shell.

What I came up with, after being inspired by your post, was a simple CGI that uses evel to do just that. Because the CGI is running in a trusted environment, we don't have to worry about people running malicous code ('cos if they do, I know where they live ;-)

So without further ado, here's my own simple solution (note that the name of the textarea element on the HTML form that sits in front of this code should be "code"):

#!/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/</&lt;/g; $code =~ s/>/&gt;/g; # add line numbers # make sure we have a \r\n at the end of the code $code .= "\r\n"; $code =~ s(.*\r\n)("<li>$&")ge; return $code; } # Print the top portion of the HTML page sub printTop { my $code = HTMLize($in{'code'}); print <<"END_OF_HTML" <html> <head> <title>WebQA TNT: Perl 101 - PerlRun</title> <style> OL { margin-top : 0%; margin-bottom : 0%; } CODE { margin-top : 0%; margin-bottom : 0%; } PRE { margin-top : 0%; margin-bottom : 0%; } </style> </head> <body bgcolor=white> <h1>WebQA TNT: Perl 101 - PerlRun</h1> <hr> <b>Evaluating code:</b> <code> <pre> <ol> $code </ol> </pre> &lt;/code> <hr> <b>Results of the evaluation are:</b> <p> <font color=blue> <pre> END_OF_HTML } # print the bottom portion of the html code sub printFoot { print <<'END_OF_HTML' </pre> </font> <hr> </body> </html> END_OF_HTML } # use an eval statement to actually execute the # statement passed in sub evaluate { package WorkSpace; $_ = shift; eval($_); print "<font color=red>Unrecognized command or syntax error.<br>$@</ +font><br>\n" if $@; print "<br>\n"; package main; }

In reply to Re: Using perl to teach programming by Anonymous Monk
in thread Using perl to teach programming by Falkkin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.