Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I 'require' a Perl library addressed by a path returned by a subroutine?

Tags:

perl

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.

like image 912
Frerich Raabe Avatar asked Oct 11 '17 14:10

Frerich Raabe


1 Answers

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.

like image 110
amon Avatar answered Oct 25 '22 19:10

amon