good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re: Persistent data structures -- why so complicated? (updated)by haukex (Archbishop) |
on Mar 11, 2021 at 18:53 UTC ( [id://11129468]=note: 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:
Here's some code using Mojo::Pg showing INSERT, SELECT, and UPDATE:
For a database that doesn't even require a server, Mojo::SQLite has 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.
* 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 Section
Seekers of Perl Wisdom
|
|