Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The thread "Is there any cache mechanism for running perl script" might be an interesting read, where the replies show how to store data structures using Storable, JSON, or Path::Class (the latter only for simple arrays). As for editing a file "in-place", I showed several variations of that in this node.

Note that, with modern databases supporting JSON*, and with modern Perl modules, databases are IMHO pretty nice to work with too. For example, given a Postgres table:

CREATE TABLE students ( name TEXT NOT NULL PRIMARY KEY, hometown TEXT NOT NULL, grade TEXT, data JSONB NOT NULL DEFAULT '{}'::jsonb );

Here's some code using Mojo::Pg showing INSERT, SELECT, and UPDATE:

use Mojo::Pg; my $pg = Mojo::Pg->new('postgres://localhost:54321/testing') ->password('BarFoo'); $pg->db->insert('students', { name => 'Bob Kowalski', hometown => 'Vero Beach, FL', data => { -json => { hobbies => [ 'ham radio', 'Python programming', 'running' ], } }, }); $pg->db->insert('students', { name => 'Kranessa Evans', hometown => 'Dallas, TX', data => { -json => { hobbies => [ 'Perl programming', 'writing', 'polo' ], } }, }); my $res = $pg->db->select('students')->expand; while ( my $rec = $res->hash ) { if ( grep {/perl/i} @{ $rec->{data}{hobbies} } ) { $pg->db->update('students', { grade=>'A' }, { name=>$rec->{name} } ); } } $res->finish;

For a database that doesn't even require a server, Mojo::SQLite has pretty much exactly the same a very similar API as the above (Edit: though I haven't used JSON in SQLite yet).

Update 2: I've modified the connection string in the above to be more useful than using the postgres superuser - and normally one wouldn't hardcode the password of course, see e.g. ~/.pgpass. The following is how I spun up the test database. I'm using port 54321 instead of the default 5432.

$ docker run --rm -p54321:5432 --name pgtestdb -e POSTGRES_PASSWORD=Fo +oBar -d postgres:13 # wait a few seconds for it to start $ echo "CREATE USER $USER PASSWORD 'BarFoo'; CREATE DATABASE testing; +GRANT ALL PRIVILEGES ON DATABASE testing TO $USER;" | psql postgresql +://postgres:FooBar@localhost:54321 $ psql postgres://localhost:54321/testing # log in and create the above table # run the above Perl script $ PGPASSWORD=BarFoo psql postgres://localhost:54321/testing -c 'SELECT + * FROM students' $ docker stop pgtestdb

* Update 3: One more thought here: I'm definitely not advocating just dumping everything in an unstructured JSON blob - one should still try to follow the rules of good database design and normalization as much as possible. But sometimes there are cases where nested data structures can be an advantage, in which case having support in the database for them can be very useful. Update 4: In that regard, erix's reply below!


In reply to Re: Persistent data structures -- why so complicated? (updated) by haukex
in thread Persistent data structures -- why so complicated? by davebaker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-03-29 06:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found