]>
iEval git - data-dump-sexp.git/blob - Sexp.pm
da4867095cdbaa006da951340d1fc3fa94696a15
1 package Data
::Dump
::Sexp
;
6 use parent qw
/Exporter/;
8 our @EXPORT = qw
/dump_sexp/;
9 our @EXPORT_OK = @EXPORT;
11 our $VERSION = '0.002';
14 use Data
::SExpression
;
15 use Scalar
::Util qw
/reftype looks_like_number/;
23 } elsif (looks_like_number
$expr) {
27 $escaped =~ s
,\\,\\\\,g
;
28 $escaped =~ s
,",\\",g
;
37 my $acc = '(' . dump_sexp
($car);
38 while (eval { $cdr->isa('Data::SExpression::Cons') }) {
41 $acc .= ' ' . dump_sexp
($car);
44 $acc .= ' . ' . dump_sexp
($cdr);
51 '(' . join (' ', map { dump_sexp
($_) } @
$expr). ')'
56 my @alist = map { Data
::SExpression
::cons
$_, $expr->{$_} } sort keys %$expr;
63 my $type = reftype
$expr;
64 if (eval { $expr->can('to_sexp') }) {
65 dump_sexp
$expr->to_sexp
66 } elsif (eval { $expr->isa('Data::SExpression::Symbol') }) {
68 } elsif (eval { $expr->isa('Data::SExpression::Cons') }) {
70 } elsif (!defined $type) {
72 } elsif ($type eq 'ARRAY') {
74 } elsif ($type eq 'HASH') {
76 } elsif ($type eq 'SCALAR' || $type eq 'REF' || $type eq 'LVALUE') {
79 croak
"Cannot dump value of type $type as sexp"
90 Data::Dump::Sexp - convert arbitrary scalars to s-expressions
95 use Data::SExpression qw/cons/;
97 say dump_sexp "yes"; # "yes"
98 say dump_sexp [1, "yes", 2]; # (1 "yes" 2)
99 say dump_sexp { b => 5, a => "yes"} # (("a" . "yes") ("b" . 5))
103 B<This module is not well-tested, proceed with caution>.
105 Data::Dump::Sexp converts Perl structures to S-expressions.
107 The conversion rules are as follows:
113 A blessed object with a B<to_sexp> method is replaced with the result
114 of calling the method, and this procedure is restarted.
118 An instance of L<Data::SExpression::Symbol> is converted to a symbol.
122 An instance of L<Data::SExpression::Cons> is converted to a cons cell
123 (like C<(A . B)>), a proper list (like C<(A B C)>) or an improper list
124 (like C<(A B . C)>), where A, B, C are S-expressions.
128 undef is converted to the empty list.
132 A defined scalar that looks like a number is left as-is.
136 A defined scalar that does not look like a number is surrounded by
137 double quotes after any backslashes and double quote characters are
138 escaped with a backslash.
142 An arrayref is converted to a proper list.
146 A hashref is converted to an alist, which is a proper list of cons
147 cells (like C<((A . B) (C . D) (E . F))>).
151 A scalarref or a reference to another ref is dereferenced and this
152 procedure is restarted.
156 Anything else (coderef, regexp, filehandle, format, globref, version
157 string) causes an exception to be raised.
161 A single function is exported by default:
165 =item B<dump_sexp> I<$expr>
167 Given any Perl scalar, convert it to a S-expression and return the
174 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
176 =head1 COPYRIGHT AND LICENSE
178 Copyright (C) 2018 by Marius Gavrilescu
180 This library is free software; you can redistribute it and/or modify
181 it under the same terms as Perl itself, either Perl version 5.24.1 or,
182 at your option, any later version of Perl 5 you may have available.
This page took 0.074203 seconds and 3 git commands to generate.