Created
November 19, 2013 20:23
-
-
Save ecounysis/7551915 to your computer and use it in GitHub Desktop.
Rudimentary REPL for PERL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use warnings; | |
@_history=(); | |
$_prompt1 = "perl> "; | |
$_prompt2 = "... "; | |
print "\nPERL interactive\n"; | |
_printlicense(); | |
print "\n\n$_prompt1"; | |
while (<>) | |
{ | |
chomp; | |
if ($_comm) | |
{ | |
$_comm = $_comm." ".$_; | |
} | |
else | |
{ | |
$_comm = $_; | |
} | |
if ($_ eq "HIST") | |
{ | |
$_i=$#_history; | |
while ($_i>=0) | |
{ | |
$_j=$_i+1; | |
print $_j . " : ". $_history[$_i]."\n"; | |
$_i-=1; | |
} | |
$_comm=""; | |
print "\n$_prompt1"; | |
} | |
elsif ($_ eq "LICENSE") | |
{ | |
$_comm = ""; | |
_printlicense(); | |
print "\n$_prompt1"; | |
} | |
elsif ($_ eq "SAVE") | |
{ | |
print "Enter file name: "; | |
chomp($_fh=<>); | |
$_pwd=`pwd`; | |
chomp($_pwd); | |
$_fh="$_pwd/$_fh"; | |
open FH, ">$_fh"; | |
print "Saving to ... $_fh"; | |
print "\n"; | |
for $_item (@_history) | |
{ | |
print FH "$_item;\n"; | |
} | |
close FH; | |
$_comm=""; | |
print "\n$_prompt1"; | |
} | |
else | |
{ | |
if ($_ =~ /.*;;$/) | |
{ | |
eval("\$_it = eval('". $_comm . "');"); | |
if ($_comm !~ /^\W*(print|sub) .*$/) | |
{ | |
print " $_it"; | |
} | |
print $@; | |
push @_history, $_comm; | |
print "\n$_prompt1"; | |
$_comm = ""; | |
} | |
else | |
{ | |
print $_prompt2; | |
} | |
} | |
} | |
sub _printlicense { | |
print "Copyright 2013, Eric Christensen\n\n"; | |
print q~ | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of this software and associated documentation files (the | |
"Software"), to deal in the Software without restriction, including | |
without limitation the rights to use, copy, modify, merge, publish, | |
distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so, subject to | |
the following conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL ERIC CHRISTENSEN BE LIABLE FOR ANY | |
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
Except as contained in this notice, the name of Eric Christensen shall | |
not be used in advertising or otherwise to promote the sale, use or | |
other dealings in this Software without prior written authorization | |
from Eric Christensen.~; | |
print "\n\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment