El módulo Storable proporciona dos rutinas: Storable::freeze y Storable::thaw que codifican y decodifican una estructura de datos arbitraria a un formato binario. Obsérvese el siguiente ejemplo:
lhp@nereida:~$ perl -de 0 main::(-e:1): 0 DB<1> use Storable qw(freeze thaw) DB<2> $x = { a=>[1..5], b=>{t=>4.5, r=>'hola'} } DB<3> $e = freeze($x) DB<4> x $e 0 "\cD\cG\cD1234\cD\cD\cD\cH\cC\cB\c@\c@\c@\cD\cB\cE\c@\c@\c@\cH\cH\cH\cH\cH\cA\ c@\c@\c@a\cD\cC\cB\c@\c@\c@\cJ\cDhola\cA\c@\c@\c@r\cG\c@\c@\c@\c@\c@\c@\cR\@\cA\c@\ c@\c@t\cA\c@\c@\c@b" DB<5> $a = thaw($e) DB<6> x $a 0 HASH(0x843f6d8) 'a' => ARRAY(0x850a300) 0 1 1 2 2 3 3 4 4 5 'b' => HASH(0x843f588) 'r' => 'hola' 't' => 4.5
Los objetos cola proveídos por Thread::Queue::Any son listas thread-safe que heredan de Thread::Queue : cualquier thread puede añadir o extraer elementos a/de la cabecera sin peligro de que se produzcan race-conditions.
Thread::Queue
estriba en que podemos
insertar una referencia a una estructura de datos arbitraria.
29 sub enqueue { 30 shift->SUPER::enqueue( Storable::freeze( \@_ ) ); 31 }de manera que dequeue devuelve todos los valores que fueron insertados enqueued juntos:
37 sub dequeue { 38 @{Storable::thaw( shift->SUPER::dequeue )}; 39 }
lhp@nereida:~$ perl -MThread::Queue::Any -e 'print map {$INC{$_}."\n" if /Queue/ } keys %INC' /usr/share/perl/5.8/Thread/Queue.pm /usr/local/share/perl/5.8.4/Thread/Queue/Any.pm lhp@nereida:~$ cat -n /usr/local/share/perl/5.8.4/Thread/Queue/Any.pm | head -58 1 package Thread::Queue::Any; 2 3 # Make sure we inherit from Thread::Queue 4 # Make sure we have version info for this module 5 # Make sure we do everything by the book from now on 6 7 @ISA = qw(Thread::Queue); 8 $VERSION = '0.07'; 9 use strict; 10 11 # Make sure we have Storable 12 # Make sure we have queues 13 14 use Storable (); # no need to pollute namespace 15 use Thread::Queue (); # no need to pollute namespace 16 17 # Allow for synonym for dequeue_dontwait 18 19 *dequeue_nb = \&dequeue_dontwait; 20 21 # Satisfy -require- 22 23 1; 24 25 #--------------------------------------------------------------------------- 26 # IN: 1 instantiated object 27 # 2..N parameters to be passed as a set onto the queue 28 29 sub enqueue { 30 shift->SUPER::enqueue( Storable::freeze( \@_ ) ); 31 } 32 33 #--------------------------------------------------------------------------- 34 # IN: 1 instantiated object 35 # OUT: 1..N parameters returned from a set on the queue 36 37 sub dequeue { 38 @{Storable::thaw( shift->SUPER::dequeue )}; 39 } 40 41 #--------------------------------------------------------------------------- 42 # IN: 1 instantiated object 43 # OUT: 1..N parameters returned from a set on the queue 44 45 sub dequeue_dontwait { 46 return unless my $ref = shift->SUPER::dequeue_nb; 47 @{Storable::thaw( $ref )}; 48 } 49 50 #--------------------------------------------------------------------------- 51 # IN: 1 instantiated object 52 # OUT: 1..N parameters returned from a set on the queue 53 54 sub dequeue_keep { 55 # return unless my $ref = shift->SUPER::dequeue_keep; # doesn't exist yet 56 return unless my $ref = shift->[0]; # temporary 57 @{Storable::thaw( $ref )}; 58 }