一边翻过perlmonks教程

时间:2017-08-26 15:41:31

标签: perl

我以为我理解了所有内容,但我想我不明白$ max->()和$ temp_score = $ max->()之间的区别..为什么打印出指针地址而后者打印出来实际价值?

use warnings;
use strict;

sub get_max {
    my $max = $_[0];

    return sub {
        for (@_) {
            $max = $_ if ! defined $max || $_ > $max;   
        }
    print "inside of get_max is max currently is $max\n";
        return $max;
    };
}


my $max = get_max();
while (<DATA>){
    chomp;
    $max->($_);
       my $temp_score = $max->();
        print ">>>>>>>>>>>>>>>>>>>>>>>>inside while loop, max is $max->()\n";
        print ">>>>>>>>>>>>>>>>>>>>>>>>inside while loop, max2 is $temp_score\n";
}

my $high_score = $max->();

print "high_score is $high_score\n";
__END__
90
91
92
87

1 个答案:

答案 0 :(得分:3)

  

为什么打印出指针地址......

我假设你引用了这一行的输出:

    print ">>>>>>>>>>>>>>>>>>>>>>>>inside while loop, max is $max->()\n";

在这种情况下,它打印$max而不是$max->(),因为它只在变量中扩展了字符串,但没有调用该函数。因此,它会产生$max的值,这是一个函数指针,后跟字符串->()

 >>>>>>>>>>>>>>>>>>>>>>>>inside while loop, max is CODE(0x242cf30)->()