in reply to Re: Perl data notation
in thread Perl data notation

<pedantic>
That makes it possible to eval them back to existence.
This holds for basic data structures, but fails for three cases (I know of):
  1. Repeated entries for the same reference
  2. Subroutine references
  3. Objects with hidden lexical content
That first one is the most common; consider
perl -MData::Dumper -e 'my $ref = []; print Dumper eval Dumper [$ref, + $ref];
which outputs
$VAR1 = [ [], undef ];
and (thankfully) fails under strict.

</pedantic>


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^3: Perl data notation
by Anonymous Monk on Jul 15, 2014 at 16:06 UTC

    It may not solve all the problems you mentioned, but:

    $ perl -MData::Dump=pp -le '$ref = []; print pp eval pp [$ref, $ref];' do { my $a = [[], 'fix']; $a->[1] = $a->[0]; $a; }
Re^3: Perl data notation
by ikegami (Patriarch) on Jul 25, 2014 at 04:02 UTC

    D::D can handle #1. You just gotta use Purity=1

    D::D can handle #2. You just gotta use Deparse=1

Re^3: Perl data notation
by davido (Cardinal) on Jul 15, 2014 at 22:25 UTC

    I believe Storable will handle subrefs, but the eval problem doesn't go away. Storable does provide a way for the user to get between the input and the eval though. And of course Storable's frozen output is anything but legible.


    Dave

      For the truly motivated, B::Deparse can serialize into a legible format.

      use strict; use B::Deparse; my $deparse = B::Deparse->new(); my $func = sub { my %hash = @_; return $hash{a}; }; my $body = $deparse->coderef2text($func); print $body;
      You could condition the tree by crawling it and clobbering anything with reftype CODE with sprintf 'sub %s', $deparse->coderef2text($hash{key}). But this still kills closures, and plus is a terrible, terrible idea.

      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re^3: Perl data notation
by Anonymous Monk on Jul 19, 2014 at 09:09 UTC

    Again, it doesn't solve all the problems you mentioned, but:

    $ perl -MData::Dumper -le '$Data::Dumper::Deparse=1; \ print Dumper eval Dumper sub { $x++; print "Hello"; }' $VAR1 = sub { ++$x; print 'Hello'; };