Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Resolving 'prototype mismatch' warning

by 1nickt (Canon)
on Feb 15, 2016 at 03:16 UTC ( [id://1155222]=perlquestion: print w/replies, xml ) Need Help??

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

Learned monks,

I am working on a test script:

#!/usr/bin/perl use strict; use warnings; use Test::More; my @compressors = qw/ Foo::Bar Compress::Snappy Compress::LZ4 /; for my $compressor ( @compressors ) { note " Testing with $compressor"; if ( ! eval "use $compressor; 1;" ) { note " Cannot load $compressor - skipping tests"; next; } note " Ready"; }
Output:
# Testing with Foo::Bar # Cannot load Foo::Bar - skipping tests # Testing with Compress::Snappy # Ready # Testing with Compress::LZ4 Prototype mismatch: sub main::compress ($) vs ($;$) at /perl5/perlbrew +/perls/perl-5.22.1/lib/5.22.1/Exporter.pm line 66. at (eval 8) line 1. Prototype mismatch: sub main::decompress ($) vs ($;$) at /perl5/perlbr +ew/perls/perl-5.22.1/lib/5.22.1/Exporter.pm line 66. at (eval 8) line 1. Prototype mismatch: sub main::uncompress ($) vs ($;$) at /perl5/perlbr +ew/perls/perl-5.22.1/lib/5.22.1/Exporter.pm line 66. at (eval 8) line 1. # Ready

I understand why the warning is printed (the two modules have identical APIs), and I know I could work around it by making separate test files, but that's not desirable. Also, it's just a warning and the subs work fine when the second module is loaded, but the warnings are undesirable.

I have searched but not found a way to clear the symbol table so the second time through the loop the subs can be imported without the warning. I can't use any non-core modules for the solution. Any help greatly appreciated!

The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re: Resolving 'prototype mismatch' warning
by jdporter (Paladin) on Feb 15, 2016 at 06:51 UTC

    Bleh. Those modules are automatically importing names into main? Turn that crap off!

    Update: It's worse than I thought. Both of those modules do EXPORT rather than EXPORT_OK. Ugh. Have you ever wanted to strangle a programmer with his own entrails?

    Well, so... remember that Exporter::import imports the names into your current namespace, which is only main by default. So you could set up a separate namespace for each test. That would be one good way to avoid namespace pollution/collisions.

    I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.

      GotToBTru suggested in the CB to use the () feature of use to prevent ->import being called:

      ... eval "use $modulename (); 1"; ...

      That way, the pollution should be somewhat prevented.

      Solved: Thanks jdporter and choroba and corion (for help in CB).

      By declaring a package name I can avoid the namespace collision:

      if ( $compressor ) { my $namespace = 'My::' . $compressor; if ( ! eval "package $namespace; use $compressor; 1;" ) { die "Could not load $compressor - $@"; } else { ... } }
      And then I can access the imported subs through my namespace by forming the full sub name into a string and by turning off strict refs:
      } else { my $compress = "My::$compressor\::compress"; no strict 'refs'; my $compressed = &$compress( $raw ); use strict 'refs'; }

      The way forward always starts with a minimal test.
Re: Resolving 'prototype mismatch' warning
by LanX (Saint) on Feb 15, 2016 at 03:51 UTC
    You are free to disable this specific warning.

    Personally I wouldn't trust any tests run with cluttered namespaces and potential side effects.

    So why don't you just spawn a new clean Perl process for each tested module?

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-23 21:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found