Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I thought it would be neat to use the perlver module to automatically detect and "use" the minimum version of Perl. But "use" does not seem to accept variables (tried many ways), while "require" takes vars but "Can't locate 5.nnn in @INC". Is this sort of thing even possible? Thanks
#!/usr/bin/perl use strict; use warnings; BEGIN { goto DONE unless eval { require Perl::MinimumVersion }; my $v = Perl::MinimumVersion->new(__FILE__); $v = $v->minimum_version; # use $v; # no way jose, can't eval either # require $v; # Can't locate 5.nnn in @INC # warn $v; # FYI DONE: }

Replies are listed 'Best First'.
Re: vars in use and require
by ikegami (Patriarch) on Sep 22, 2019 at 03:23 UTC
    die "Perl version $v required, only $]\n" if $] < $v;

    Update: I had the arguments to the comparison reversed.

Re: vars in use and require
by jcb (Parson) on Sep 21, 2019 at 22:36 UTC

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

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

      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?)

Re: vars in use and require
by stevieb (Canon) on Sep 21, 2019 at 23:08 UTC

    Might I ask why you'd want to do this? Is it just a curiosity?

      Because I'm looking at ways to integrate and automate perlver in development and possibly distribution.