Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Perl doesn't give error when module is not found 2

by Anonymous Monk
on Mar 26, 2024 at 10:53 UTC ( [id://11158519]=perlquestion: print w/replies, xml ) Need Help??

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

In Perl I have a script 'a.pl' that calls a module 'MyModule.pm' and inside that module
there's a call with function 'call()' to another module 'MyModule2.pm' function '$authorize'
as 'MyModule2::authorize()' but MyModule2 is not loaded with 'use MyModule2' inside MyModule.pm.

But I get no error "module cannot be found" when running the script 'perl a.pl' which calls 'MyModule.pm' function 'call()' which calls 'MyModule2::authorize()'

Only if I run 'perl MyModule.pm' I get error "MyModule2.pm not found".

  • Comment on Perl doesn't give error when module is not found 2

Replies are listed 'Best First'.
Re: Perl doesn't give error when module is not found 2
by choroba (Cardinal) on Mar 26, 2024 at 11:16 UTC
    I can't reproduce the problem:

    a.pl

    #!/usr/bin/perl use warnings; use strict; use lib '.'; use MyModule; MyModule::call();

    MyModule.pm

    package MyModule; use warnings; use strict; sub call { MyModule2::authorize(); } __PACKAGE__

    MyModule2.pm

    package MyModule2; use warnings; use strict; sub authorize { print "auth\n"; } __PACKAGE__

    Running ./a.pl results in the following error:

    Undefined subroutine &MyModule2::authorize called at MyModule.pm line +6.

    Update: It starts working when I include

    use MyModule2;
    into MyModule.pm or a.pl: but the correct way is the former, as the script shouldn't care about what its dependecies need to load.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Perl doesn't give error when module is not found 2
by LanX (Saint) on Mar 26, 2024 at 11:09 UTC
    Unclear.

    SSCCE ?

    Did you use strict and warnings in all modules?

    What if you call Perl with -W switch?

    And please don't name different threads with the same title.

    Either keep both posts in the same thread or come up with better titles.

    Get a log in, so you can fix it next time.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

Re: Perl doesn't give error when module is not found 2
by Jenda (Abbot) on Mar 27, 2024 at 10:52 UTC
    1. If module A uses something from module B, then there OUGHT TO BE use B; inside A.pm. Fullstop. Modules should not expect someone else to load their dependencies for them. There is no need to "optimize" and use a module just one. Perl is clever enough to notice the module had already been loaded and it doesn't load it twice.
      The second time Perl only calls the module's import() subroutine to allow it to export the default list of functions into the calling namespace.
    2. You are NOT supposed to run modules directly. Very few modules support that and likely even fewer should.
    3. Modules and namespaces are related, but they are NOT the same thing. You can have several namespaces right inside your a.pl script and define the myModule2:authorize right there. It's actually quite common for modules to contain several namespaces. One of them SHOULD match the name of the file, but there's often several others inside.

    Jenda
    1984 was supposed to be a warning,
    not a manual!

Re: Perl doesn't give error when module is not found 2
by ikegami (Patriarch) on Mar 27, 2024 at 12:48 UTC

    The following is wrong:

    perl MyModule.pm

    To load a module, use

    perl -I . -e'use MyModule;'
      "wrong" in the sense of "why would anybody do this?", yes, but not "syntactically wrong" or "won't work" - see my other reply

        Can cause problems. They're not equivalent, and the latter is how the module is normally loaded.

Re: Perl doesn't give error when module is not found 2
by soonix (Canon) on Mar 27, 2024 at 14:59 UTC
    Jenda and ikegami discourage running modules directly. In general, they are right, but:
    Just because I (and they, obviously ;-)) don't know a legitimate reason to use modulinos and the like, doesn't mean there is no legitimate reason - and what isn't legitimate for one, might well be for someone else.

      Just because I (and they, obviously ;-)) don't know a legitimate reason to use modulinos and the like

      Though modulinos are too sneaky/clever for my tastes ("scripts should use modules, not pretend to be modules"), they do have some well-respected fans, led by their illustrious inventor brian_d_foy, as described in more detail at modulino References.

      My preference is keep my script mainlines as short as is practicable, with all the heavy lifting done in (unit-tested) module/s.

      👁️🍾👍🦟

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11158519]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-09-14 13:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (21 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.