This very short program turns perl data into lisp data. I used it at work to turn the Data::Dumper output of a program into lisp data. This is mostly because the lisp is easier on the eyes than Data::Dumper.
#!/usr/local/bin/perl
use strict;
no strict 'vars';
use warnings;
use Scalar::Util 'reftype';
undef $/;
my $s = eval <STDIN>;
print $@ ? $@ : "(pp `".lisp( $s ).")\n";
sub lisp {
# This is an utter, ugly hack. Please pretend you never saw it.
my $thing = shift;
my $type = reftype( $thing );
no warnings 'uninitialized';
if ( 'HASH' eq $type ) {
# Returns an association list
# ((KEY . VALUE)
# (KEY . VALUE)
# (KEY . VALUE))
return '('. join( '', map { "($_ . ".lisp($thing->{$_}).')'} s
+ort keys %$thing ) .')';
}
elsif ( 'ARRAY' eq $type ) {
# Returns a list
# (ELT ELT ELT ELT ELT)
return '(' . join( ' ', map lisp( $_ ), @$thing ) . ')';
}
elsif ( not defined $type ) {
# Returns bare numbers. I don't think I need to deal with unde
+rflow.
return $thing if looks_like_float( $thing );
# Returns bare numbers if I think they won't overflow
return $thing if
length $thing <= 9
and looks_like_integer( $thing );
return qq["\Q$thing\E"];
}
# Freely admit I can't handle anything fancy.
die "Can't handle $type";
}
sub looks_like_float { shift =~ /^-?\d+\.\d+\z/ }
sub looks_like_integer { shift =~ /^-?\d+\z/ }