in reply to vars in use and require

The use is evaluated at compile time; try eval STRING instead of eval BLOCK:

BEGIN { # ... eval q{use $v}; die $@ if $@; }

Replies are listed 'Best First'.
Re^2: vars in use and require
by haukex (Archbishop) on Sep 21, 2019 at 23:50 UTC

    I was going to suggest something along the lines of BEGIN { eval "use $]; 1" or die $@ } instead (Bug in eval in pre-5.14), but it doesn't actually work to enable features:

    $ perl -e 'BEGIN { eval "use $]; 1" or die $@ } say "foo"' String found where operator expected at -e line 1, near "say "foo"" (Do you need to predeclare say?) syntax error at -e line 1, near "say "foo"" Execution of -e aborted due to compilation errors.

    Because the scope of feature is limited to the lexical scope.

    So, Anonymous Monk, to echo stevieb: Why do you (think you) need this? (Why try to do something dynamically that is determined statically / at compile time?)