1Data::Serializer::CookbUosoekr(3C)ontributed Perl DocumeDnattaat:i:oSnerializer::Cookbook(3)
2
3
4
6 Cookbook - Examples of how to use Data::Serializer
7
9 Data::Serializer::Cookbook is a collection of solutions for using
10 Data::Serializer.
11
13 Unless otherwise specified, all examples can be assumed to begin with:
14
15 use Data::Serializer;
16
17 my $serializer = Data::Serializer->new();
18
19 Some examples will show different arguments to the new method, where
20 specified simply use that line instead of the simple form above.
21
23 Fort hose who want a straight pass through to the underlying
24 serializer, where nothing else is done (no encoding, encryption,
25 compression, etc) there is Data::Serializer::Raw(3).
26
27 These begin like this:
28
29 use Data::Serializer::Raw;
30
31 my $raw_serializer = Data::Serializer::Raw->new();
32
34 You wish to encrypt your data structure, so that it can only be decoded
35 by someone who shares the same key.
36
37 Solution
38 $serializer->secret('mysecret');
39
40 my $encrypted_hashref = $serializer->serializer($hash);
41
42 ... (in other program) ...
43
44 $serializer->secret('mysecret');
45
46 my $clear_hash = $serializer->deserializer($encrypted_hash);
47
48 Note: You will have to have the Crypt::CBC module installed for this
49 to work.
50
52 You wish to compress your data structure to cut down on how much disk
53 space it will take up.
54
55 Solution
56 $serializer->compress(1);
57
58 my $compressed_hashref = $serializer->serializer($hash);
59
60 ... (in other program) ...
61
62 my $clear_hash = $serializer->deserializer($compressed_hash);
63
64 Note: You will have to have the Compress::Zlib module installed for
65 this to work. Your mileage will vary dramatically depending on what
66 serializer you use. Some serializers are already fairly compact.
67
69 You need to write a program that can read in data serialized in a
70 format other than Data::Serializer. For example you need to be able to
71 be able to process data serialized by XML::Dumper.
72
73 Solution
74 use Data::Serializer::Raw;
75
76 my $xml_raw_serializer = Data::Serializer::Raw->(serializer => 'XML::Dumper');
77
78 my $hash_ref = $xml_raw_serializer->deserialize($xml_data);
79
81 Data::Serializer
82 You need to write a program that can write out data in a format other
83 than Data::Serializer. Or said more generically you need to write out
84 data in the format native to the underlying serializer. For our
85 example we will be exporting data using XML::Dumper format.
86
87 Solution
88 ues Data::Serializer::Raw;
89
90 my $xml_raw_serializer = Data::Serializer::Raw->(serializer => 'XML::Dumper');
91
92 my $xml_data = $xml_raw_serializer->serialize($hash_ref);
93
95 You have data serialized by php that you want to convert to xml for use
96 by other programs.
97
98 Solution
99 use Data::Serializer::Raw;
100
101 my $xml_raw_serializer = Data::Serializer::Raw->(serializer => 'XML::Dumper');
102
103 my $php_raw_serializer = Data::Serializer::Raw->(serializer => 'PHP::Serialization');
104
105 my $hash_ref = $php_raw_serializer->deserialize($php_data);
106
107 my $xml_data = $xml_raw_serializer->serialize($hash_ref);
108
110 You have a program that you run every 10 minutes, it uses SNMP to pull
111 some counters from one of your routers. You want your program to keep
112 the counters from the last run so that it can see how much traffic has
113 passed over a link since it last ran.
114
115 Solution
116 # path to store our serialized data
117 # be paranoid, use full paths
118 my $last_run_datafile = '/full/path/to/file/lastrun.data';
119
120 #We keep our data as a hash reference
121 my $last_data = $serializer->retrieve($last_run_datafile);
122
123 #Pull in our new data through 'pull_data()';
124 my $new_data = query_router($router);
125
126 #run comparison code
127 run_comparison($last_data,$new_data);
128
129 $serializer->store($new_data);
130
132 Neil Neely <neil@neely.cx>.
133
135 Copyright (c) 2001-2011 Neil Neely. All rights reserved.
136
137 This program is free software; you can redistribute it and/or modify it
138 under the same terms as Perl itself.
139
141 Data::Serializer(3)
142 Data::Serializer::Raw(3)
143
144
145
146perl v5.36.0 2023-01-20 Data::Serializer::Cookbook(3)