NAME

Data::SExpression -- Parse Lisp S-Expressions into perl data structures.

SYNOPSIS

use Data::SExpression;

my $ds = Data::SExpression->new;

$ds->read("(foo bar baz)");          # [\*::foo, \*::bar, \*::baz]

my @sexps;
my $sexp;
while(1) {
    eval {
        ($sexp, $text) = $ds->read($text);
    };
    last if $@;
    push @sexps, $sexp;
}

$ds = Data::SExpression->new(fold_alists => 1);

$ds->read("((top . 4) (left . 5)");  # {\*::top => 4, \*::left => 5}

LISP-LIKE CONVENIENCE FUNCTIONS

These are all generic methods to make operating on cons's easier in perl. You can ask for any of these in the export list, e.g.

use Data::SExpression qw(cons consp);

cons CAR CDR

Convenience method for Data::SExpression::Cons->new(CAR, CDR)

consp THING

Returns true iff THING is a reference to a Data::SExpression::Cons

scalarp THING

Returns true iff THING is a scalar -- i.e. a string, symbol, or number

METHODS

new [\%args]

Returns a new Data::SExpression object. Possibly args are:

fold_lists

If true, fold lisp lists (e.g. "(1 2 3)") into Perl listrefs, e.g. [1, 2, 3]

Defaults to true.

fold_alists

If true, fold lisp alists into perl hashrefs. e.g.

"((fg . red) (bg . black) (weight . bold))"

would become

{
    \*fg       => \*red,
    \*bg       => \*black,
    \*weight   => \*bold
}

Alists will only be folded if they are a list of conses, all of which have scalars as both their car and cdr (See "scalarp" in Data::SExpression::Cons)

This option implies "fold_lists"

Defaults to false.

read STRING

Parse an SExpression from the start of STRING, or die if the parse fails.

In scalar context, returns the expression parsed as a perl data structure; In list context, also return the part of STRING left unparsed. This means you can read all the expressions in a string with:

my @sexps;
my $sexp;
while(1) {
    eval {
        ($sexp, $text) = $ds->read($text);
    };
    last if $@;
    push @sexps, $sexp;
}

This method converts Lisp SExpressions into perl data structures by the following rules:

Numbers and Strings become perl scalars

Lisp differentiates between the types; perl doesn't.

Symbols become globrefs in main::

This means they become something like \*main::foo, or \*::foo for short. To convert from a string to a symbol, you can use "qualify_to_ref" in Symbol, with "main" as the package.

Conses become Data::SExpression::Cons objects

See Data::SExpression::Cons for how to deal with these. See also the fold_lists and fold_alists arguments to "new".

Quotation is parsed as in scheme

This means that "'foo" is parsed like "(quote foo)", "`foo" like "(quasiquote foo)", and ",foo" like "(unquote foo)".

AUTHOR

Nelson Elhage <nelhage@mit.edu>