イテレータの狂気:
#!/usr/bin/env perl
use autodie;
use strict; use warnings;
my $keyfile_contents = join("\n", 'A' .. 'J');
my $valuefile_contents = join("\n", map ord, 'A' .. 'E');
# Use get_iterator($keyfile, $valuefile) to read from physical files
my $each = get_iterator(\ ($keyfile_contents, $valuefile_contents) );
my %hash;
while (my ($k, $v) = $each->()) {
$hash{ $k } = $v;
}
use YAML;
print Dump \%hash;
sub get_iterator {
my ($keyfile, $valuefile) = @_;
open my $keyf, '<', $keyfile;
open my $valf, '<', $valuefile;
return sub {
my $key = <$keyf>;
return unless defined $key;
my $value = <$valf>;
chomp for grep defined, $key, $value;
return $key => $value;
};
}
出力:
C:\temp> yy
---
あ:65
乙: 66
子:67
日: 68
え:69
ふ:~
ガ:〜
ひ:~
私:〜
じ:~