1package Storable::AMF3; 2use strict; 3use warnings; 4use Fcntl qw(:flock); 5use subs qw(freeze thaw); 6use Exporter 'import'; 7use Carp qw(croak); 8BEGIN{ 9 our $VERSION; 10 $VERSION='1.00' unless $INC{'Storable/AMF0.pm'}; 11}; 12use Storable::AMF0 (); 13 14# Items to export into callers namespace by default. Note: do not export 15# names by default without a very good reason. Use EXPORT_OK instead. 16# Do not simply export all your public functions/methods/constants. 17 18our %EXPORT_TAGS = ( 19 'all' => [ 20 qw( 21 freeze thaw dclone retrieve lock_retrieve lock_store lock_nstore store nstore 22 ref_clear ref_lost_memory 23 deparse_amf new_amfdate perl_date 24 new_date 25 parse_option 26 parse_serializator_option 27 ) 28 ] 29); 30 31our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); 32 33our @EXPORT = qw(); 34 35sub retrieve($) { 36 my $file = shift; 37 my $lock = shift; 38 39 open my $fh, "<:raw", $file or croak "Can't open file \"$file\" for read."; 40 flock $fh, LOCK_SH if $lock; 41 my $buf; 42 sysread $fh, $buf, (( sysseek $fh, 0, 2 ), sysseek $fh, 0,0)[0] ; 43 return thaw($buf); 44} 45 46sub lock_retrieve($) { 47 $_[1] = 1; 48 goto &retrieve; 49} 50 51sub store($$) { 52 my ( $object, $file, $lock ) = @_; 53 54 my $freeze = \freeze($object); 55 unless (defined $$freeze ){ 56 croak "Bad object"; 57 } 58 else { 59 open my $fh, ">:raw", $file or croak "Can't open file \"$file\" for write."; 60 flock $fh, LOCK_EX if $lock; 61 print $fh $$freeze if defined $$freeze; 62 }; 63} 64 65sub lock_store($$) { 66 $_[2] = 1; 67 goto &store; 68} 69{{ 70 no warnings 'once'; 71 *nstore = \&store; 72 *lock_nstore = \&lock_store; 73}}; 741; 75__END__ 76# Below is stub documentation for your module. You'd better edit it! 77 78=head1 NAME 79 80Storable::AMF3 - serializing/deserializing AMF3 data 81 82=head1 SYNOPSIS 83 84 use Storable::AMF3 qw(freeze thaw); 85 86 $amf3 = freeze($perl_object); 87 $perl_object = thaw($amf3); 88 89 90 # Store/retrieve to disk amf3 data 91 92 store $perl_object, 'file'; 93 $restored_perl_object = retrieve 'file'; 94 95 96 use Storable::AMF3 qw(nstore freeze thaw dclone); 97 98 99 # Advisory locking 100 use Storable::AMF3 qw(lock_store lock_nstore lock_retrieve) 101 lock_store \%table, 'file'; 102 lock_nstore \%table, 'file'; 103 $hashref = lock_retrieve('file'); 104 105 # Deparse one object 106 use Storable::AMF0 qw(deparse_amf); 107 108 my( $obj, $length_of_packet ) = deparse_amf( my $bytea = freeze($a1) . freeze($a) ... ); 109 110 - or - 111 $obj = deparse_amf( freeze($a1) . freeze($a) ... ); 112 113=cut 114 115=head1 DESCRIPTION 116 117This module is (de)serializer for Adobe's AMF3 (Action Message Format ver 3). 118This is only module and it recognize only AMF3 data. 119Almost all function implemented in C for speed. 120And some cases faster then Storable( for me always) 121 122=cut 123 124=head1 EXPORT 125 126 None by default. 127 128=cut 129 130=head1 FUNCTIONS 131=cut 132 133=over 134 135=item freeze($obj) 136 --- Serialize perl object($obj) to AMF3, and return AMF data 137 138=item thaw($amf3) 139 --- Deserialize AMF data to perl object, and return the perl object 140 141=item store $obj, $file 142 --- Store serialized AMF3 data to file 143 144=item nstore $obj, $file 145 --- Same as store 146 147=item retrieve $obj, $file 148 --- Retrieve serialized AMF3 data from file 149 150=item lock_store $obj, $file 151 --- Same as store but with Advisory locking 152 153=item lock_nstore $obj, $file 154 --- Same as lock_store 155 156=item lock_retrieve $file 157 --- Same as retrieve but with advisory locking 158 159=item dclone $file 160 --- Deep cloning data structure 161 162=item ref_clear $obj 163 --- cleaning arrayref and hashref. (Usefull for destroing complex objects) 164 165=item ref_lost_memory $obj 166 --- test if object contain lost memory fragments inside. 167 (Example do { my $a = []; @$a=$a; $a}) 168 169=item deparse_amf $bytea 170 --- deparse from bytea one item 171 Return one object and number of bytes readed 172 if scalar context return object 173 174=item parse_option 175=item parse_serializator_option 176 generate option scalar for freeze/thaw/deparse_amf 177 See L<Storable::AMF0> for complete list of options 178 179=back 180 181 182=head1 LIMITATION 183 184At current moment and with restriction of AMF0/AMF3 format referrences to scalar are not serialized, 185and can't/ may not serialize tied variables. 186 187=head1 SEE ALSO 188 189L<Data::AMF>, L<Storable>, L<Storable::AMF3>, L<Storable::AMF> 190 191=head1 AUTHOR 192 193Anatoliy Grishaev, <grian at cpan dot org> 194 195=head1 THANKS 196 197 Alberto Reggiori. ( basic externalized object support ) 198 Adam Lounds. ( tests and some ideas and code for boolean support ) 199 200=head1 COPYRIGHT AND LICENSE 201 202Copyright (C) 2011 by A. G. Grishaev 203 204This library is free software; you can redistribute it and/or modify 205it under the same terms as Perl itself, either Perl version 5.8.8 or, 206at your option, any later version of Perl 5 you may have available. 207=cut 208