develooper Front page | perl.perl5.porters | Postings from February 2020

Re: "require" change of behavior in 5.16 or slightly before?

Thread Previous | Thread Next
From:
Tony Cook
Date:
February 19, 2020 23:27
Subject:
Re: "require" change of behavior in 5.16 or slightly before?
Message ID:
20200219232743.GH18378@mars.tony.develop-help.com
On Wed, Feb 19, 2020 at 02:46:51PM -0700, Philip Prindeville wrote:
> Hi,
> 
> I have some build scripts that used to work in perl-5.10 and when we updated to perl-5.16 one in particular broke badly.
> 
> It does a:
> 
> require $config_file;
> 
> and two observations: (1) if $config_file is an absolute path, it no longer finds the file (I can see from a security standpoint that not being able to include arbitrary files anywhere on the system might be good…),

  tony@mars:.../git/perl$ cat ~/play/require.pl
  print "Hello\n";

  1;
  tony@mars:.../git/perl$ perl -e '$cfg = "/home/tony/play/require.pl"; require $cfg'
  Hello

  tony@mars:.../git/perl$ perl -v

  This is perl 5, version 28, subversion 1 (v5.28.1) built for x86_64-linux-gnu-thread-multi
  ...

Do you have a more complete example?

> and (2) if I change that too “… || die …” then the die doesn’t happen.
> 
> Were there changes along the way to how “require” behaves?  And what’s the workaround?

It seems like require isn't failing in your example, or perl thinks
the module is already loaded, for example:

  # the second require succeeds without loading
  $ perl -e '$cfg = "/home/tony/play/require.pl"; require $cfg; require $cfg;'
  Hello

Perl sets $INC{supplied name} to indicate the file is already loaded,
and if the previous load was successful, require will succeed without
trying to reload the file.  If you clear that entry the second load
happens:

  tony@mars:.../git/perl$ perl -e '$cfg = "/home/tony/play/require.pl"; require $cfg; delete $INC{$cfg}; require $cfg;'
  Hello
  Hello

This isn't new behaviour:

  tony@mars:.../git/perl$ ~/perl/5.10.0-debug/bin/perl -e '$cfg = "/home/tony/play/require.pl"; require $cfg; require $cfg;'
  Hello
  tony@mars:.../git/perl$ ~/perl/5.8.9-thr/bin/perl -e '$cfg = "/home/tony/play/require.pl"; require $cfg; require $cfg;'
  Hello

If you want to unconditionally load a file you probably want "do":

  $ perl -e '$cfg = "/home/tony/play/require.pl"; do $cfg; do $cfg;'
  Hello
  Hello


Tony

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About