On Sat, 7 Apr 2018, at 07:24, Bill Stephenson wrote:
> I’ve been working on a “comments” feature for my “CherryPC blog”.
>
> I don’t want readers to have to make a user account to comment so I’m
> wanting to use a perl script on the server side that has the user
> credentials in the $url variable below.
>
> This is the code I’m using to update the document with the comment.
>
> # Convert the JSON to a perl object
>
> my $data_structure = decode_json(`curl -X GET $url`);

Hi Bill,

I think all the responses so far have given you a piece of the puzzle, but not 
the whole. Hopefully this completes the picture.

TLDR just use Store::CouchDB,  and all of the conversion will "just happen" 
directly

https://metacpan.org/release/Store-CouchDB

[snip]

given this JSON object as an example of what gets converted to/from perl 
objects:

GET /testy/data HTTP/1.1
Host: localhost:5984
Accept: application/json
Accept-Encoding: gzip, deflate
User-Agent: bat/0.1.0

HTTP/1.1 200 OK
Content-Length : 111
Cache-Control : must-revalidate
Server : CouchDB/1.7.1 (Erlang OTP/19)
Etag : "1-75710f70ac64e43a12fbc76ca3305852"
Date : Sat, 07 Apr 2018 21:58:56 GMT
Content-Type : application/json

{
  "_id": "data",
  "_rev": "1-75710f70ac64e43a12fbc76ca3305852",
  "object": {
    "nested": true
  },
  "array": [
    1,
    "2",
    false,
    null
  ]
}

The JSON should be self-explanatory but its useful to see how the same data is 
represented in perl.

So here's a very short perl program to show you how much easier this is using 
Store::CouchDB and the automatic perl<->JSON conversions.

#!/usr/bin/env perl

use Modern::Perl;
use Store::CouchDB;
use Data::Printer caller_info => 1;

# connect to our couch and existing db
my $c = Store::CouchDB->new({host => 'localhost', db => 'testy'});

# fetch a JSON doc and auto-convert it to a perl object
my $doc = $c->get_doc('data');

# dump out the perl object we got back
# it's not so scary
p $doc;

# look up a simple plain text value
say $doc->{'_id'};

# nested objects can be traversed by name
# true in JSON is mapped to 1 in perl (truthy)
# and will round-trip correctly
say $doc->{'object'}->{'nested'};
exit 0;


The output follows; in your terminal it will be in glorious colour:

 ./relax.pl 
Printing in line 15 of ./relax.pl:
\ {
    _id      "data",
    _rev     "1-75710f70ac64e43a12fbc76ca3305852",
    array    [
        [0] 1,
        [1] 2,
        [2] JSON::PP::Boolean  {
            Parents       Types::Serialiser::BooleanBase
            public methods (0)
            private methods (0)
            internals: 0
        },
        [3] undef
    ],
    object   {
        nested   JSON::PP::Boolean  {
            Parents       Types::Serialiser::BooleanBase
            public methods (0)
            private methods (0)
            internals: 1
        }
    }
}
data
1

note how the doc id "data" is a simple string, and the JSON::PP::Boolean 
object, and undef, is used to ensure that the JSON true, false, and null values 
respectively have an equivalent definition in perl that can be safely 
round-tripped back to JSON.

> From what I understand "use utf8” forces the all data to be utf-8...

I highly recommend getting chromatic's book 
https://pragprog.com/book/swperl/modern-perl-fourth-edition and reading it 
immediately. There's a free online version 
http://www.onyxneon.com/books/modern_perl/index.html but it's well worth the 
pennies. A few hours reading this will power up your perl skills enormously I 
think.

> If anyone has any ideas and/or advice on how to deal with this I’d sure
> appreciate them. I’ve pretty much ran out of them at this point.

TLDR use a library and then you can skip all the pain of shelling out to 
external programs. Also this is a very risky strategy - sanitising user input 
that is passed to/from curl and so forth is dangerous, especially on the 
internet. The same goes for date/time handling, look on metacpan.org and find a 
library to do the heavy lifting for you.

Finally, http://mojolicious.org/ is a very good modern perl web framework. In a 
future incarnation of your site/app it may be very helpful, but I suspect 
you've already done enough not to want to start from scratch.

Good luck!

A+
Dave

Reply via email to