current endeavors : 100 pushups

Exercise 11: Perl Code

#!/usr/bin/perl

#------------------------------------------------------------------------------
# Author: Daniel Bigham
# Date:   September 2008
#------------------------------------------------------------------------------
# Perl script to render a digraph using GraphViz.
#------------------------------------------------------------------------------

use CGI;

my $cgi = new CGI();

#------------------------------------------------------------------------------
# Web form mode
#------------------------------------------------------------------------------
if ($cgi->param('mode') eq "form")
{
    print "Content-type: text/html\n\n";

    print <<HTML;
<html>
<body>
<form method=POST action=/graph/graph.pl>
<textarea name="graph" rows=20 cols=70>
</textarea><br>
<input type=submit value="Submit >>" style='border:1px solid #888888; background:#dddddd; margin-top:4px; font-family:arial; font-size:10pt;'>
</form>
</body>
</html>
HTML

exit 0;
}

#------------------------------------------------------------------------------
# Rendering mode (default)
#------------------------------------------------------------------------------

my $input = $cgi->param('graph');

# Hack for getting single quoted entities to work
$input =~ s/<single_quote_open>/"'/;
$input =~ s/<single_quote_close>/'"/;

if (open(PROG, '|"C:\Program Files\Graphviz2.20\bin\neato.exe" -Tgif -o C:\Inetpub\graph\output.gif'))
{
    print PROG $input . "\n";
    close PROG;
}

if (open(FILE, 'C:\Inetpub\graph\output.gif'))
{
    print "Content-type: image/gif\n\n";
    binmode FILE;
    while(<FILE>)
    {
      print $_;
    }
    close FILE;
}
else
{
    print "Content-type: text/html\n\n";
    print "ERROR: Couldn't open image file.<br>\n";
}