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

Hi,

I have 2 minimalistic pm files:
# SYPH1.pm package SYPH1; $SYPH1::VERSION = '0.01'; 1;
and
# SYPH2.pm package SYPH2; if(defined $INC{'SYPH1.pm'}) {print "SYPH1.pm loaded\n"} else {print "SYPH1.pm NOT loaded\n"} 1;
On Windows, I run:
D:\>set PERL5OPT= D:\>perl -MSYPH1 -MSYPH2 -e "" SYPH1.pm loaded D:\>perl -MSYPH1 -e "use SYPH2" SYPH1.pm loaded D:\>set PERL5OPT=-MSYPH1 D:\>perl -e "use SYPH2" SYPH1.pm loaded D:\>perl -MSYPH2 -e "" SYPH1.pm NOT loaded D:\>
And that all makes perfect sense to me, except for the last command.
For some reason SYPH2 gets loaded before before SYPH1.
Is there some way to get the module specified by "PERL5OPT" loaded before the module(s) specified by "-M" ?

Cheers,
Rob

Replies are listed 'Best First'.
Re: Unexpected behaviour with PERL5OPT
by hippo (Archbishop) on Oct 24, 2024 at 12:36 UTC

    https://perldoc.perl.org/perlrun#ORDER-OF-APPLICATION covers this explicitly and currently states:

    After normal processing of -M switches from the command line, all the -M switches in PERL5OPT are extracted. They are processed from left to right, i.e. the same as those on the command line.

    So, the answer is no unless you explicitly insert it by running perl $PERL5OPT -MSYPH2 -e "" or similar.


    🦛

      So, the answer is no ...

      Yeah ... I was looking for a way of ensuring that whenever perl was run, then a specific module would be loaded before all other modules.
      I was hoping that loading that module at the beginning of perl5opt would achieve that end, but now I see that alone is not guaranteed to work.

      Actually, I was just wanting Test/Builder.pm to load Math/Ryu.pm (if Math::Ryu was installed).
      The usual way to do that would be to have Builder.pm do eval{require Math::Ryu} but that causes t/Legacy/dont_overwrite_die_handler.t to fail a test (in the Test::Simple test suite) if Math::Ryu was not found.
      Seems a funny thing to be causing a test to fail - but if they're testing for it, then one assumes it's important. (Shrug.)

      So the next approach was to have Builder.pm check whether "Math/Ryu.pm" was in %INC ... and that's where it became important that Math::Ryu (if available) was loaded before Test::Builder.
      Maybe that can be done simply enough ... but there's a few "what-ifs" to process when thinking through that.

      And it's just an exercise that's not really going to lead anywhere.
      Test::More can throw out some really stupid and irritating diagnostics like:
      >perl -MTest::More -le "cmp_ok(0.1 ** 2, '==', 0.01, 'T1'); done_testi +ng();" not ok 1 - T1 # Failed test 'T1' # at -e line 1. # got: 0.01 # expected: 0.01 1..1 # Looks like you failed 1 test of 1.
      So I've patched Test/Builder.pm to be (eg) capable of providing:
      >perl -MMath::Ryu -MTest::More -le "cmp_ok(0.1 ** 2, '==', 0.01, 'T1') +; done_testing();" not ok 1 - T1 # Failed test 'T1' # at -e line 1. # got: 0.010000000000000002 # expected: 0.01 1..1 # Looks like you failed 1 test of 1.
      I'll submit a PR when (if) I get the detail of involving Math::Ryu reliably sorted out.
      But this irritating Test::More behaviour crops up only rarely, and no-one cares about it, anyway.

      hippo, thanks for digging up the documentation.

      Cheers,
      Rob
        If you compare floats in a test, you should compare for "close enough", not ==. See float in Test2::V0​.

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