NAME
Data::Dump::Sexp - convert arbitrary scalars to s-expressions
SYNOPSIS
use Data::Dump::Sexp;
use Data::SExpression qw/cons/;
say dump_sexp 5; # 5
say dump_sexp "yes"; # "yes"
say dump_sexp [1, "yes", 2]; # (1 "yes" 2)
say dump_sexp { b => 5, a => "yes"} # (("a" . "yes") ("b" . 5))
DESCRIPTION
This module is not well-tested, proceed with caution.
Data::Dump::Sexp converts Perl structures to S-expressions.
The conversion rules are as follows:
A blessed object with a to_sexp method is replaced with the result of calling the method, and this procedure is restarted.
An instance of Data::SExpression::Symbol is converted to a symbol.
An instance of Data::SExpression::Cons is converted to a cons cell (like
(A . B)
), a proper list (like(A B C)
) or an improper list (like(A B . C)
), where A, B, C are S-expressions.undef is converted to the empty list.
A defined scalar that looks like a number is left as-is.
A defined scalar that does not look like a number is surrounded by double quotes after any backslashes and double quote characters are escaped with a backslash.
An arrayref is converted to a proper list.
A hashref is converted to an alist, which is a proper list of cons cells (like
((A . B) (C . D) (E . F))
).A scalarref or a reference to another ref is dereferenced and this procedure is restarted.
Anything else (coderef, regexp, filehandle, format, globref, version string) causes an exception to be raised.
A single function is exported by default:
- dump_sexp $expr
-
Given any Perl scalar, convert it to a S-expression and return the sexp as a string.
AUTHOR
Marius Gavrilescu, <marius@ieval.ro>
COPYRIGHT AND LICENSE
Copyright (C) 2018 by Marius Gavrilescu
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.24.1 or, at your option, any later version of Perl 5 you may have available.