- 论坛徽章:
- 0
|
原本是帮一个朋友面试而写的,顺便公开出来,请多指教。
- #!/\perl/\bin
- # Copyright (C) 2008 luo chuanting<[email]rocenting@gmail.com[/email]>
- # Release under GPL license
- use strict;
- my $test_switch = 1;
- main();
- sub main {
- if ($test_switch == 1) {
- test_1();
- test_2();
- }
- }
- sub gen_iterator_1 {
- my $count = shift @_;
- while ($count < 10) {
- $count++;
- return (sub {gen_iterator_1($count)}, [$count, "gen_iterator_1"])
- }
- return;
- }
- sub cal_value {
- my $next_fn = shift @_;
- my $e;
- while (($next_fn, $e) = $next_fn->()) {
- print "$e->[1]: $e->[0]\n";
- }
- }
- sub test_1 {
- my $next_fn = sub {gen_iterator_1(0);};
- my $e;
- while (($next_fn,$e ) = $next_fn->()) {
- print "$e->[1]: $e->[0]\n";
- }
- }
- sub test_2 {
- my $count = 5;
- sub gen_iterator_2 {
- while ($count < 10) {
- $count++;
- return (\&gen_iterator_2, [$count, "gen_iterator_2"])
- }
- return;
- }
- cal_value(\&gen_iterator_2);
- }
复制代码
[ 本帖最后由 rocenting 于 2008-12-24 13:06 编辑 ] |
|