Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Persistent data structures -- why so complicated?

by Discipulus (Canon)
on Mar 11, 2021 at 18:13 UTC ( [id://11129466]=note: print w/replies, xml ) Need Help??


in reply to Persistent data structures -- why so complicated?

Hello davebaker,

probably I missed the point but if you are programming in perl I do not see the complexity using Storable. Anyway first of all take a look to a recent thread: Banal Configuration Languages

That said you can have a lot of options beside Storable. The basic Data::Dumper dumps structures that can be eval -ed to have the datastructure back.

If you like more human readable solutions there is YAML or the very common outside perl world JSON

I used something like perl -MYAML -MStorable -e "print Dump @{retrieve ($ARGV[0])};" and perl -e "use YAML (LoadFile); use Storable qw(nstore); @ar = LoadFile($ARGV[0]); nstore(\@ar, $ARGV[1])" to translate YAML and Storable formats.

A plethora of Config::* modules are also available.

You can have comma separated data, in external files but also after the __DATA__ token.. So.. many many options for every taste.

HTH

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2:Persistent data structures -- why so complicated?
by davebaker (Pilgrim) on Mar 11, 2021 at 22:33 UTC

    Thanks! I think I got freaked out by the Perl Cookbook's lengthy example as to how to use file locking with Storable, but now that I've looked at the outstanding documentation for the module I see there's even a lock_store function (and lock_nstore for a more portable though slightly slower solution) that apparently would replace those details (and lock_retrieve). Sometimes the Cookbook (2003) tells me more than I need to know, but Time Marches On and I forget that the state of the art is different than when the second edition of the Cookbook was published 18 years ago. It's still the book I typically reach for first.

    Would a comma-separated data format work in my example? Each student "record" has a key that holds multiple values (the student's hobbies), so I'm thinking there isn't a key-to-scalar-value relationship that's required for a CSV format.

      Of course CSV would suffice for the example you gave, but it will be a lot more complicated if you have nested structures.

      You can combine all the given examples, e.g. use the Database example by using DBD::CSV or next level, tie that CSV database handle with Tie::Hash::DBD and use your hash without changing the rest of your program.

      One advice: don't underestimate how "simple" CSV is. *do* use a module, like Text::CSV_XS and/or Text::CSV.

      For the hobbies, you would either require a serialization like Sereal or Storable.

      If hobbies is the *only* field with multiple values, just drop them at the end of the record, so you can restore them lik

      use 5.14.2; use warnings; use Text::CSV_XS "csv"; use Data::Peek; my $data = csv (in => *DATA); my @hdr = @{shift @$data}; my $n = $#hdr; my @students = map { my %h; @h{@hdr} = splice @$_, 0 , $n; $h{hobbies} = [ @$_ ]; \%h, } @$data; DDumper \@students; __END__ name,home_town,age,hobbies Angie Green,Denver,12,Horses,Painting,Karaoke,Ham Radio Jamie Brown,London,34 Mark White,Madrid,56,Cooking,Perl,Running Mary Black,Dublin,30,Baking,Singing,Swimming,Programming Perl

      -->

      [ { age => '12', hobbies => [ 'Horses', 'Painting', 'Karaoke', 'Ham Radio' ], home_town => 'Denver', name => 'Angie Green' }, { age => '34', hobbies => [], home_town => 'London', name => 'Jamie Brown' }, { age => '56', hobbies => [ 'Cooking', 'Perl', 'Running' ], home_town => 'Madrid', name => 'Mark White' }, { age => '30', hobbies => [ 'Baking', 'Singing', 'Swimming', 'Programming Perl' ], home_town => 'Dublin', name => 'Mary Black' } ]
      use Text::CSV_XS "csv";

      Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-20 02:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found