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
Tony Cook
February 19, 2020 23:27
Re: "require" change of behavior in 5.16 or slightly before?
Message ID:
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/
  print "Hello\n";

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

  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 $cfg; require $cfg;'

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

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

This isn't new behaviour:

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

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

  $ perl -e '$cfg = "/home/tony/play/"; do $cfg; do $cfg;'


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About