Search code examples
perlreferencesubroutine

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?


Solution

  • You're missing a semicolon after the closing brace in the statement $imapSub = sub { ... }, so whatever you put after that is unexpected and causes a syntax error.