Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Perl data notation

by rje (Deacon)
on Jul 15, 2014 at 14:44 UTC ( [id://1093716] : perlquestion . print w/replies, xml ) Need Help??

rje has asked for the wisdom of the Perl Monks concerning the following question:

Has anyone used Perl's data notation as a web-consumable data format?

Yes, it does have a lot of overlap with JSON. It looks a bit like JSON, too. The bare keywords and the => are just syntactic sugar, but in some cases the data looks cleaner than JSON, doesn't it? And it can (potentially) have referenced data shared between two structures -- perhaps not critically important for the web, but potentially useful. Right?

An app I had written could dump its output to XML, JSON, YAML, or Perl's data notation... it's not difficult to write an emitter for it. And I know the ecological niche is small, and JSON pretty much has it sewn up. I'm just asking for the sake of asking. In other words, don't bite my head off.

Replies are listed 'Best First'.
Re: Perl data notation
by davido (Cardinal) on Jul 15, 2014 at 14:54 UTC

    If by Perl's data notation you mean Data::Dumper type output, that's what Data::Dumper does; it serializes data structures in such a way that they would be perfectly legal as source code. That makes it possible to eval them back to existence.

    But consider the implications of string eval: You would be executing your input. In the context of web work, you would be eval'ing (compiling and running) user input! That is the biggest of all possible security risks.

    So to do it safely, you would need to come up with a module that parses the input similar to the way in which a JSON parser parses its input, and then returns a living data structure. And by the time you've done that, you may as well just use JSON; a format that everyone knows and understands, with robust parsing solutions available.

    Even though JavaScript could "eval" most JSON input, in practice it's not done that way, for the same reason I've described above. Instead, it parses JSON into a data structure using a JSON parser, never actually compiling and executing it.


    Dave

      <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.

        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; }

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

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

        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

        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'; };
Re: Perl data notation
by perlfan (Vicar) on Jul 15, 2014 at 15:37 UTC
    I thought his was called "PSON" (jk).

    You are right that it could be useful for this, but what is more useful is the fact that there is a similar looking standard and that that it is *not* XML ;).

    If you're looking of better serialization performance, you may want to also checkout out BSON and Sereal.

      I thought his was called "PSON" (jk).

      Your think correctly :)

      Acme::PSON - PSON(PerlScript Object Notation) Module

        Is that pronounced with a long E or a short I?
Re: Perl data notation (PSON/JSON/SafestUndumper)
by Anonymous Monk on Jul 15, 2014 at 20:40 UTC

    Sure, why not :) I did it long before crockford published JSON, long before anyone was using "AJAX" and there was a jQuery, when we called it DHTML :)

Re: Perl data notation
by Anonymous Monk on Jul 15, 2014 at 18:53 UTC
    I would suggest sticking with a well-accepted standard, such as JSON, and always use one of the well-accepted encoder/decoder rings to deal with it.

      Q: Has anyone ever used ...
      A: I would suggest sticking with ...

      non-responsiveness worthy of sundialsvc4