#!/usr/local/bin/perl use strict; no strict 'vars'; use warnings; use Scalar::Util 'reftype'; undef $/; my $s = eval ; 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->{$_}).')'} sort 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 underflow. 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/ }