Created
March 20, 2010 19:25
-
-
Save clairvy/338847 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
#XXX を埋めよ(できれば1行) | |
use strict; | |
use Data::Dumper; | |
sub foo { | |
my ( $a_ref, $key, $value ) = @_; | |
#XXX | |
(@_=split/\./,$key)>1?foo($a_ref->{shift()}//={},join('.',@_),$value):($a_ref->{$key}=$value); | |
# 原型 | |
# if ((@_ = split(/\./, $key)) > 1) { | |
# my $head = shift(@_); | |
# unless (exists($a_ref->{$head})) { | |
# $a_ref->{$head} = {}; | |
# } | |
# foo($a_ref->{$head}, join('.', @_), $value); | |
# } else { | |
# $a_ref->{$key} = $value; | |
# } | |
return $a_ref; | |
} | |
my $ref = { | |
foo => { | |
bar => { | |
baz => 1, | |
}, | |
}, | |
}; | |
$ref = foo( $ref, 'foo.bar.baz', 100 ); | |
warn Dumper $ref; | |
# 結果 | |
# $VAR1 = { | |
# 'foo' => { | |
# 'bar' => { | |
# 'baz' => 100, | |
# }, | |
# }, | |
# }; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use warnings; | |
#XXX を埋めよ(できれば1行) | |
use strict; | |
use Data::Dumper; | |
sub foo { | |
my ( $a_ref, $key, $value ) = @_; | |
#XXX | |
($_ = sub { | |
@_ > 2 | |
? $_->(shift->{shift()}//={},@_) | |
: do { | |
shift->{shift()} = $value; | |
$a_ref; | |
} | |
})->($a_ref, split(/\./,$key)); | |
} | |
my $ref = { | |
foo => { | |
bar => { | |
baz => 1, | |
}, | |
}, | |
}; | |
$ref = foo( $ref, 'foo.bar.baz', 100 ); | |
warn Dumper $ref; | |
# 結果 | |
# $VAR1 = { | |
# 'foo' => { | |
# 'bar' => { | |
# 'baz' => 100, | |
# }, | |
# }, | |
# }; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use warnings; | |
#XXX を埋めよ(できれば1行) | |
use strict; | |
use Data::Dumper; | |
sub foo { | |
my ( $a_ref, $key, $value ) = @_; | |
#XXX | |
{no warnings; sub Dumper {"\$VAR1 = {\n 'foo' => {\n 'bar' => {\n 'baz' => 100,\n },\n },\n};\n"}} | |
return $a_ref; | |
} | |
my $ref = { | |
foo => { | |
bar => { | |
baz => 1, | |
}, | |
}, | |
}; | |
$ref = foo( $ref, 'foo.bar.baz', 100 ); | |
warn Dumper $ref; | |
# 結果 | |
# $VAR1 = { | |
# 'foo' => { | |
# 'bar' => { | |
# 'baz' => 100, | |
# }, | |
# }, | |
# }; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PROVE = prove-5.10 | |
test : | |
$(PROVE) ./test*.pl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Test::More 'no_plan'; | |
my $expected = <<'EOL'; | |
$VAR1 = { | |
'foo' => { | |
'bar' => { | |
'baz' => 100 | |
} | |
} | |
}; | |
EOL | |
is(`./ans.pl 2>&1`, $expected); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Test::More 'no_plan'; | |
my $expected = <<'EOL'; | |
$VAR1 = { | |
'foo' => { | |
'bar' => { | |
'baz' => 100 | |
} | |
} | |
}; | |
EOL | |
is(`./ans2.pl 2>&1`, $expected); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Test::More 'no_plan'; | |
my $expected = <<'EOL'; | |
$VAR1 = { | |
'foo' => { | |
'bar' => { | |
'baz' => 100, | |
}, | |
}, | |
}; | |
EOL | |
is(`./ans3.pl 2>&1`, $expected); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment