Data::...okbook(3pm)
π NAME π
Cookbook β literally a whole lil' recipe box of examples showing you how to use Data::Serializer. It's giving "User Contributed Perl Documentation", and the page itself goes by Data::...okbook(3pm), bestie. π
β¨ DESCRIPTION β¨
Data::Serializer::Cookbook is a collection of solutions for using Data::Serializer. That's it, that's the vibe β it's the "how do I actually do the thing" pile of answers. π
π CONVENTIONS π
Unless it says otherwise, assume every single example starts off like this:
use Data::Serializer;
my $serializer = Data::Serializer->new();
Some examples are gonna hand different arguments to the new method β when that happens, just swap that line in for the simple one above. No big deal, no cap. π
π CONVENTIONS for Raw Access π
For those of you who want a straight pass through to the underlying serializer, where nothing else gets done β no encoding, no encryption, no compression, none of it β there's Data::Serializer::Raw(3). Raw. Unfiltered. Main character energy. π¦
These ones kick off like this:
use Data::Serializer::Raw;
my $raw_serializer = Data::Serializer::Raw->new();
π Encrypting your data π
So you wanna encrypt your data structure so that, like, the only person who can decode it is someone who shares the same key. Obsessed. Secretive. Very spy-movie.
Solution
$serializer->secret('mysecret');
my $encrypted_hashref = $serializer->serializer($hash);
... (in other program) ...
$serializer->secret('mysecret');
my $clear_hash = $serializer->deserializer($encrypted_hash);
Note: You will have to have the Crypt::CBC module installed for this to work. π
ποΈ Compressing your data ποΈ
You wanna compress your data structure to cut down on how much disk space it'll take up. Tiny data, big savings, we love to see it. β¨
Solution
$serializer->compress(1);
my $compressed_hashref = $serializer->serializer($hash);
... (in other program) ...
my $clear_hash = $serializer->deserializer($compressed_hash);
Note: You will have to have the Compress::Zlib module installed for this to work. Your mileage will vary dramatically depending on what serializer you use. Some serializers are already fairly compact. π
π₯ You want to read in data serialized outside of Data::Serializer π₯
You need to write a program that can read in data serialized in a format other than Data::Serializer. For example, you need to be able to be able to process data serialized by XML::Dumper. Bestie, we've all been there. π
Solution
use Data::Serializer::Raw;
my $xml_raw_serializer = Data::Serializer::Raw->(serializer => 'XML::Dumper');
my $hash_ref = $xml_raw_serializer->deserialize($xml_data);
π€ You want to write serialized data in a form understood outside of Data::Serializer π€
You need to write a program that can write out data in a format other than Data::Serializer. Or, said more generically, you need to write out data in the format native to the underlying serializer. For our example we will be exporting data using XML::Dumper format. Slay. π
Solution
ues Data::Serializer::Raw;
my $xml_raw_serializer = Data::Serializer::Raw->(serializer => 'XML::Dumper');
my $xml_data = $xml_raw_serializer->serialize($hash_ref);
π You want to convert data between two different serializers native formats π
You have data serialized by php that you want to convert to xml for use by other programs. Glow-up for your data fr fr. β¨
Solution
use Data::Serializer::Raw;
my $xml_raw_serializer = Data::Serializer::Raw->(serializer => 'XML::Dumper');
my $php_raw_serializer = Data::Serializer::Raw->(serializer => 'PHP::Serialization');
my $hash_ref = $php_raw_serializer->deserialize($php_data);
my $xml_data = $xml_raw_serializer->serialize($hash_ref);
πΎ Keeping data persistent between executions of a program. πΎ
You have a program that you run every 10 minutes, it uses SNMP to pull some counters from one of your routers. You want your program to keep the counters from the last run so that it can see how much traffic has passed over a link since it last ran. Iconic little cron-job behaviour, honestly. π
Solution
# path to store our serialized data
# be paranoid, use full paths
my $last_run_datafile = '/full/path/to/file/lastrun.data';
#We keep our data as a hash reference
my $last_data = $serializer->retrieve($last_run_datafile);
#Pull in our new data through 'pull_data()';
my $new_data = query_router($router);
#run comparison code
run_comparison($last_data,$new_data);
$serializer->store($new_data);
π AUTHOR π
Neil Neely <neil@neely.cx>.
πΈ COPYRIGHT πΈ
Copyright (c) 2001-2011 Neil Neely. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
π«Ά SEE ALSO π«Ά
okay that's the whole cookbook bestie, go forth and serialize π This lil' page is from the User Contributed Perl Documentation, riding along with perl v5.36.0 and dated 2022-10-22 β that's a wrap on Data::...okbook(3pm), mwah. ππ
π¦ where this page comes from
- debian bookworm: libdata-serializer-perl 0.65-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libdata-serializer-perl 0.65-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libdata-serializer-perl 0.65-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libdata-serializer-perl 0.65-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libdata-serializer-perl 0.65-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms