use DBI; my $dbh = ... my $sth = $dbh->prepare( 'SELECT field, offset, width' . ' FROM Source_Field' . ' WHERE source = ?;' ); my $source = 'input_file.txt'; $sth->execute($source); my $template; my @fields; while (my $column_spec = $sth->fetchrow_hashref() ) { my ($field, $offset, $width) = @$column_spec{qw(field offset width)}; $template .= "\@${offset}A$width"; push @fields, $field; } open my $reader, '<', $source; while (<$reader>) { my %value_of; my @values = unpack($template, $_); @value_of{@fields} = @values; # you've got your current record in a hash # print it or save it somewhere }