in reply to Re^2: Is require still required?
in thread Is require still required?

Except it's a really unreliable way to return a value.

This is an assertion without argument.

If it would be unreliable, it should fail sometimes - but it doesn't, only in the ways require does.

Yes, the example code is just bare-bone without any checks. It is also nonsensical because it doesn't do anything useful.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Replies are listed 'Best First'.
Re^4: Is require still required?
by haj (Vicar) on Feb 02, 2024 at 13:34 UTC
    If it would be unreliable, it should fail sometimes - but it doesn't, ....

    I am pretty sure that Toby meant "unreliable" as in "the return value depends on whether that file had already been required before".

    Let me expand his example a bit:

    use strict; use warnings; use autodie; use File::Temp qw/ tempfile /; use Data::Dump; my ($fh, $filename) = tempfile(); print $fh qq('Hello, world';\n); close $fh; my $first = require $filename; dd $first; my $second = require $filename; dd $second;

    Output:

    "Hello, world"
    1
    

      This is all stated in the documentation for require, inclusive the role of %INC:

      if (exists $INC{$filename}) { return 1 if $INC{$filename}; croak "Compilation failed in require"; }

      To import a fle more than once you have to delete from %INC of course.

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
        > To import a fle more than once you have to delete from %INC of course.

        Or use a different path to it (I've seen this in production, don't ask).

        My.pm:

        package My; use warnings; use strict; our $counter //= 10; ++$counter

        script.pl

        #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use lib './'; say require My; # 11 say require 'My.pm'; # In %INC, returns 1. say require './My.pm'; # Not in %INC, thus 12!

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]