use strict; use warnings; use Data::Dumper; my %hash; open my $fh, '<', 'hash.txt' or die "Can't open file $!"; while (my $line = <$fh>) { chomp $line; # remove trailing newline $line =~ s/^\s+//; # optional: remove leading whitespace $line =~ s/\s+$//; # optional: remove trailing whitespace next unless length($line); # ignore blank lines next if $line =~ /^#/; # optional: to allow comment lines # Format: key whitespace value (value can contain whitespace) my ($key, $value) = split ' ', $line, 2; defined($value) or die "error: no value for '$key'"; $hash{$key} = $value; } close $fh; print Dumper(\%hash);