From 617f98ad5c465bc18fe45de2095d9599d7998ada Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Sun, 28 Mar 2010 15:06:50 +0000 Subject: [PATCH] Improved codepad.pl --- modules/codepad.pl | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/modules/codepad.pl b/modules/codepad.pl index 66e5cee5..259787f2 100755 --- a/modules/codepad.pl +++ b/modules/codepad.pl @@ -1,8 +1,5 @@ #!/usr/bin/perl -# Initial rough-draft prototype proof of concept -# Once working, need to refactor and polish. - use warnings; use strict; @@ -23,12 +20,28 @@ my $code = join ' ', @ARGV; my $lang = "C"; $lang = $1 if $code =~ s/-lang=([^\b\s]+)//i; +my @languages = qw/C C++ D Haskell Lua OCaml PHP Perl Python Ruby Scheme Tcl/; + +my $found = 0; +foreach my $l (@languages) { + if(uc $lang eq uc $l) { + $lang = $l; + $found = 1; + last; + } +} + +if(not $found) { + print "$nick: Invalid language '$lang'. Supported languages are: @languages\n"; + exit 0; +} + my $ua = LWP::UserAgent->new(); $ua->agent("Mozilla/5.0"); push @{ $ua->requests_redirectable }, 'POST'; -if(not $code =~ m/\w+ main\s?\([^)]+\)\s?{/) { +if(($lang eq "C" or $lang eq "C++") and not $code =~ m/\w+ main\s?\([^)]+\)\s?{/) { $code = "int main(void) { $code ; return 0; }"; } @@ -53,7 +66,7 @@ $text =~ s/\d+<\/a>//g; if($text =~ /Output:<\/span>.+?
(.*)<\/div>.+?<\/table>/si) { $output = "$1"; } else { - $output = "No output."; + $output = "
No output.
"; } $output = decode_entities($output);