use strict; use warnings; sub serialize_array{ my $self = shift; my $aref = shift || []; my $io = shift || *STDOUT; use bytes; foreach my $e(@$aref){ if( defined $e ){ $io->print( pack("N", length $e)."D".$e ); } else{ $io->print( pack("N", 0)."U" ); } } } sub restore_array{ my $self = shift; my $io = shift || *STDIN; my $r = []; while( read($io, my $buf, 4) ){ my $len = unpack "N", $buf; read($io, my $def, 1); # defined//undef read($io, my $e, $len); push @$r, $def eq "D" ? $e : undef; } return $r; } 1;######################################################################### # Tests untenstehend use IO::String; use Data::Dumper; my $io = IO::String->new; my $m = bless{}; $m->serialize_array( [0,'foo','bar','bäh',undef,''], $io ); $io->seek(0,0); my $r = $m->restore_array($io); print Dumper $r;;