Drop the "return" keyword:
juerd@lanova:~$ perl -MO=Deparse -e'BEGIN { eval "sub foo () { return
+42; }" } print foo;'
sub BEGIN {
eval 'sub foo () { return 42; }';
}
print foo;
-e syntax OK
juerd@lanova:~$ perl -MO=Deparse -e'BEGIN { eval "sub foo () { 42 }" }
+ print foo;'
sub BEGIN {
eval 'sub foo () { 42 }';
}
print 42;
-e syntax OK
Also, there's a better way than using
eval. You can assign a code reference to a glob:
*glob = sub () { value };
juerd@lanova:~$ perl -MO=Deparse -e'BEGIN { *foo = sub () { 42 } } pri
+nt foo;'
sub BEGIN {
*foo = sub () { 42 }
;
}
print 42;
-e syntax OK