Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Using perl to teach programming

by Anonymous Monk
on Feb 13, 2001 at 03:09 UTC ( [id://58017]=note: print w/replies, xml ) Need Help??


in reply to Using perl to teach programming

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; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://58017]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 05:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found