Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Contextual find and replace large config file

by kschwab (Vicar)
on Jan 02, 2019 at 18:34 UTC ( [id://1227939]=note: print w/replies, xml ) Need Help??


in reply to Contextual find and replace large config file

"Does anyone know of a better or more generic way to do these kind of things?"

There's lots of choices for config files. JSON and YAML are popular. Your second example is pretty close to JSON already. It would look like this as JSON:

{ "ObjectType1": { "Param1": 8, "Param2": "SomeText", "NestedObject": { "Param1": 3, "Param2": "SomeText" } }, "ObjectType2": { "Param1": 10 } }

There are perl modules to parse JSON, some streaming, if you really can't load it all into memory. There's also a really nice command line utility called "jq", see some examples here.

Note that JSON doesn't support comments, which is probably the biggest complaint about it as a configuration file format.

Replies are listed 'Best First'.
Re^2: Contextual find and replace large config file
by LanX (Saint) on Jan 03, 2019 at 23:09 UTC
    > Note that JSON doesn't support comments, which is probably the biggest complaint about it as a configuration file format.

    I never noticed this - most probably because I never came into a situation to need it.

    What's surprising me, is that JSON historically started as eval'ed JS object, so why did they skip the comment feature?

    Especially since CSS inherited JS comments too.

    So I did some research to find out that Douglas Crockford disabled it deliberately, because he wanted to prevent people from hiding data there. ...

    ... well, Douglas again. :/

    Anyway, for config purpose I'd try split up the data into multiple JSON chunks and comment them, or resort to YAML, which allows JSON as subset.

    --- # Comment { "name": "John Smith", "age": 33 }

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re^2: Contextual find and replace large config file
by karlgoethebier (Abbot) on Jan 03, 2019 at 17:23 UTC
    "... JSON doesn't support comments..."

    It does if you treat them as data:

    #!/usr/bin/env perl use strict; use warnings; use JSON::Tiny qw(decode_json encode_json); use Data::Dump; my $conf = encode_json { foo => qw(bar), nose => qw(cuke), comment => qw(RTFM) }; my $hash = decode_json($conf); dd $hash; __END__ { comment => "RTFM", foo => "bar", nose => "cuke" }

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

      Comments in most languages can appear anywhere where insignificant whitespace is possible. Your approach can't transform structures that comment both on the keys and values, as in
      { "name" /* represented as "shortname" in the DB */ : "John Doe" /* full name */,
      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        “...Your approach...“

        Nobody is perfect.

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1227939]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-03-28 11:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found