my %v; # this hash holds all the data read # define the names of the "variables" to read from the file # *** in the correct order *** my @varnames = qw/this that theother/; # define where the length to read is not a default my %varlengths = ('that' => 2, 'theother' = '$v{this}*2'} foreach my $var (@varnames){ # get the length to read my $readlength; if(! exists($varlengths{$var}) $readlength = 1; # default } else { $readlength = eval($varlengths{$var}); # compute it } # read into %v if ($readlength == 1){ # read a scalar $v{$var} = unpack TEMPL1, read( $file, $readlength ); } else { # read into an anon. array $v{$var} = [ unpack TEMPL1, read( $file, $readlength ) ]; } }