I can use this to include a file a.pl
:
require 'a.pl';
Or I can use this:
$fn = 'a.pl';
require $fn;
However, a subroutine won't work:
sub fn { return 'a.pl'; }
require fn(); # This is a syntax error
Is there a syntax for allowing this? I noticed that I can work around the issue via
sub fn { return 'a.pl'; }
require eval("fn()");
...but that's not terribly pretty.
require(fn());
What a curious syntax error! Adding an extra parenthesis to disambiguate precedence fixes the problem. It seems that otherwise the require PACKAGE
form would have precedence over require EXPR
, i.e. the fn
is parsed as the bareword designating the module you want to load.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With