Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

inconsistent module access

by geoffleach (Scribe)
on Jun 24, 2022 at 21:46 UTC ( [id://11145024]=perlquestion: print w/replies, xml ) Need Help??

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

I have two modules which are structurally identical. At one point, they access IO::All::io. In one case, it goes without a hitch. In the other,
DanBongino::_get(/home/geoff/Perl/newmod/DanBongino.pm:54): 54: my $data = io( $url )->slurp; DB<10> s Undefined subroutine &DanBongino::io called at /home/geoff/Perl/newmod +/DanBongino.pm line 54. at /home/geoff/Perl/newmod/DanBongino.pm line 54. DanBongino::_get("https://feeds.megaphone.fm/WWO3519750118/") call +ed at /home/geoff/Perl/newmod/DanBongino.pm line 71 DanBongino::Get() called at NewFetchPodcast.pl line 92 Getopt::Auto::CODE(0x55a5417cc5e0)(/usr/local/lib64/perl5/5.34/Getopt/ +Auto.pm:120):
Fiddling around in the debuggger
DanBongino::_get(/home/geoff/Perl/newmod/DanBongino.pm:54): 54: my $data = io( $url )->slurp; DB<12> io('foo') Undefined subroutine &DanBongino::io called at (eval 30)[/usr/share/pe +rl5/perl5db.pl:741] line 2. DB<13> IO::All::io('foo') Can't locate object method "_package" via package "foo" (perhaps you f +orgot to load "foo"?) at /usr/share/perl5/vendor_perl/IO/All.pm line +63.
In both cases the modules are executed with this code in a given-when
{ require DanBongino; $shows = DanBongino::Get() };
use IO::All; is present in both cases.

Assistance most greatfully received.

Replies are listed 'Best First'.
Re: inconsistent module access
by choroba (Cardinal) on Jun 24, 2022 at 22:57 UTC
    I can get the weird behaviour if I use IO::All without running its import, i.e. using
    use IO::All ();

    or using require instead of use.

    Another way to skip the import is to redefine it:

    sub IO::All::import {} use IO::All;

    Without seeing the actual code, I'm not sure I can help you more.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Hmmm seems like I can cut down the test a tad :-) File t.pl ...
      use feature 'switch'; no warnings 'experimental'; $m = 'test'; given ( $m ) { when ('test') { test::Get() }; } package test; sub Get { require IO::All; $data = io(); } 1;
      % perl t.pl Undefined subroutine &test::io called at t.pl line 11.
      FWIW, changing the call to io()
      $data = IO::All::io()
      Results in a different error.
      Can't call method "_package" on an undefined value at /usr/share/perl5 +/vendor_perl/IO/All.pm line 63.

        It's what choroba said: because you're using require instead of use, the module's import method is not called, which is required for the module to work correctly.

        $data = IO::All::io() Results in a different error.

        That's because there is no sub io anywhere in the distribution. io is dynamically generated by the aforementioned import in the caller's package. The unusual error comes from the fact that the call gets sent to IO::All::AUTOLOAD(), which doesn't handle io.

        You need to say either use IO::All; or require IO::All; IO::All->import(); for the module to work correctly.

        My most esteemed brethren choroba and HaukeX already nailed down your problem.

        But you seem to be confused about the difference between use and require

        from the docs

          use Module

          Imports some semantics into the current package from the named module, generally by aliasing certain subroutine or variable names into your package. It is exactly equivalent to

          BEGIN { require Module; Module->import( LIST ); }

          except that Module must be a bareword. ...

        BEGIN happens at compile-time, if you want to use a module at run-time, you always need to call

        ->import

        too.

        It's also an FAQ, plz see

        --> What's-the-difference-between-require-and-use?

        and plz follow the various links embedded in this node to round up the picture.°

        HTH! :)

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

        °) well you could already have followed some of them before

      No joy, alas. I would be glad to share the code, but not to clutter up this space. Suggestion?
        I would be glad to share the code, but not to clutter up this space. Suggestion?

        Github is your friend for hosting longer postings that may involve more than one file. Then post the link here. Typically, there's one file that cannot be posted for security reasons, but you can post a mock-up of what it has to look like, and what values have to be supplied in order for it to run. Sometimes the attempt at making it shorter or more self-contained is worth the effort.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 12:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found