Created
June 23, 2016 16:32
-
-
Save xtetsuji/e686b9a4cb036f2c70acbcd1d7894159 to your computer and use it in GitHub Desktop.
forward 1st execution answer to right callback.
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/perl | |
use strict; | |
use warnings; | |
# こういう forward 関数(仮名)があれば | |
sub forward { | |
my $landing_cb = pop; | |
my @sub_returns = @_; | |
local $_ = $sub_returns[0]; | |
$landing_cb->(@sub_returns); | |
} | |
use Scalar::Util qw(looks_like_number); | |
my $num = shift or die "require number as 1st argument"; | |
if ( !looks_like_number($num) ) { | |
die "not number!"; | |
} | |
# こういうふうに書くものが | |
my $answer = sqrt $num; | |
print "$num の平方根は $answer です\n"; | |
# こういうふうに書ける | |
forward sqrt $num, sub { | |
print "$num の平方根は $_ です\n"; | |
}; | |
# 場合によっては読みやすい? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment