my $bps = 32; my $fmt = 3; my %template = ( "32,3" => 'd',# A double-precision float in native format. "32,1" => 'I', "32,2, => 'i', # ... add the other formats as you need ); my $rawdata = get_raw_data_from_C_library(); my $t = $template{ "$bps,$fmt" } or die "Couldn't find an unpack template for $bps BPS, format $fmt"; my @samples = unpack $template, $rawdata;