Why can't I assign this perl sub to a variable?

I'm trying to understand the convoluted execution path of the imap routine on p.158 of HOP.

This code works

# code from rng-iterator.pl
sub make_rand {
my $seed = shift || (time & 0x7fff);
print "nin make_rand, at6: seed=$seed";
return sub 
    {   $seed = (29*$seed+11111) & 0x7fff;
print "nin make_rand sub, at9: seed=$seed";
        return $seed; 
    }
}

# code adapted from HOP p.158, to make an iterator version of map
sub imap {
    my ($transform, $it) = @_;
print "nin imap, at17";
    return sub 
    {   my $next = $it->();
print "nin imap sub, at20, next=$next";
        return unless defined $next;
        $newVal = $transform->($next);
print "nin imap sub, at23, newVal=$newVal";
        return $newVal;
    }
}

# to return random number 0 .. 1
$rng = imap(sub {$_[0] / 37268}, make_rand(1)); # set seed 
print "nin main at30, rng=$rng";
while (<>) {    
    my $random = $rng->();  
    print "nin main, at 32: random=$random";
}

There seems to be no problem returning a reference to a sub (imap) to a string $rng, and using that to point to imap's sub .

I wanted to assign the sub to a string INSIDE of imap, and return the string, as here:

    $imapSub =  sub 
        {   my $next = $it->();
                print "nin imap sub, at20, next=$next";
            return unless defined $next;
            $newVal = $transform->($next);
                print "nin imap sub, at23, newVal=$newVal";
            return $newVal;
        }
    return $imapSub;

Perl reported a Syntax Error when I tried to return or print $imapSub, or even use it as an argument to ref(). It didn't complain when I assigned the sub to the variable.

It does the same even if I explicitly cast a reference the subroutine as $&sub.

Why do I get a syntax error when I attempt to use the reference?


您在语句$imapSub = sub { ... }括号后面缺少分号,因此无论您在此之后放置的是何种意外,都会导致语法错误。

链接地址: http://www.djcxy.com/p/59226.html

上一篇: 在perl中,'严格使用'会禁用重要警告

下一篇: 为什么我不能将此perl子分配给一个变量?