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

Hi, As I said, its just a wrapper over C functions, there's not much of code in it (works with Dynaloader and AutoLoader). It works fine if I use this module alone. Here is the code:
package ABC; $ABC::VERSION = '0.02'; use strict; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); require Exporter; require DynaLoader; require AutoLoader; @ISA = qw(Exporter AutoLoader DynaLoader); # Items to export into callers namespace by default. @EXPORT = qw( new_time_var dl_ascii_to_date dl_ascii_to_time dl_close_cache ); @EXPORT_OK = qw(); bootstrap ABC $VERSION;
and here is the wrapper module:
package PQR; use strict; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); require Exporter; require AutoLoader; use ABC; $PQR::VERSION = $ABC::VERSION; @ISA = qw(Exporter AutoLoader); @EXPORT = qw( new_time_var dl_ascii_to_date dl_ascii_to_time dl_close_cache ); @EXPORT_OK = qw(); sub new_time_var { return ABC::new_time_var(@_); } sub dl_ascii_to_date { return ABC::dl_ascii_to_date(@_); } sub dl_ascii_to_time { return ABC::dl_ascii_to_time(@_); } sub dl_close_cache { return ABC::dl_close_cache(@_); }
The actual subroutine signatures are:
<new_time_var>() <dl_ascii_to_date>(<string>) <dl_ascii_to_time>(<string>, <dtp>) <dl_close_cache>(<dcp>)
When I test this using a script I get the following errors:
>/u/agarwsun/test> perl -wc test.pl
Prototype mismatch: sub PQR::new_time_var () vs none at /u/agarwsun/projects/packages/datelib/lib/perl5/5.8.6/i86pc-solaris/PQR.pm line 71.
Subroutine new_time_var redefined at /u/agarwsun/projects/packages/datelib/lib/perl5/5.8.6/i86pc-solaris/PQR.pm line 67.
Prototype mismatch: sub PQR::dl_ascii_to_date ($) vs none at /u/agarwsun/projects/packages/datelib/lib/perl5/5.8.6/i86pc-solaris/PQR.pm line 76. Subroutine dl_ascii_to_date redefined at /u/agarwsun/projects/packages/datelib/lib/perl5/5.8.6/i86pc-solaris/PQR.pm line 74.
Prototype mismatch: sub PQR::dl_ascii_to_time ($$) vs none at /u/agarwsun/projects/packages/datelib/lib/perl5/5.8.6/i86pc-solaris/PQR.pm line 81.
If I do not re-export the functions in PQR, and extend from ABC, I don't get the errors related to redefining subroutines, but I still get prototype mismatch errors:
Prototype mismatch: sub ABC::new_time_var () vs none at /u/agarwsun/projects/packages/datelib/lib/perl5/5.8.6/i86pc-solaris/PQR.pm line 22.
Prototype mismatch: sub ABC::dl_ascii_to_date ($) vs none at /u/agarwsun/projects/packages/datelib/lib/perl5/5.8.6/i86pc-solaris/PQR.pm line 27.
Prototype mismatch: sub Deshaw::DateLib::dl_ascii_to_time ($$) vs none at /u/agarwsun/projects/packages/datelib/lib/perl5/5.8.6/i86pc-solaris/PQR.pm line 32.

Replies are listed 'Best First'.
Re: Creating wrapper over a wrapper (prototype mismatch)
by moritz (Cardinal) on May 05, 2010 at 10:17 UTC
    What's the prototype of wrapper::ABC?

    You can disable prototype checking by adding a & in front wrapper::ABC(@_) but that might not always do you (or the original author of wrapper::ABC) wanted.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Creating wrapper over a wrapper
by Anonymous Monk on May 05, 2010 at 10:19 UTC
    That is very poor interface design which you should accomplish using variants of Exporter.

    i get errors saying that there is a prototype mismatch between the ABC here and wrapper::ABC. Can

    You should always copy/paste error message, and post code that reproduces the error (see How (Not) To Ask A Question for more)

      Sorry, i replaced the actual question.
Re: Creating wrapper over a wrapper
by almut (Canon) on May 05, 2010 at 14:09 UTC

    Try

    use ABC ();

    i.e., don't import the original functions into your PQR namespace, if you're going to define wrapper functions of the same name.

    (When writing just "use ABC;" the functions are automatically imported because they are in the @EXPORT array (as opposed to @EXPORT_OK) — see Exporter.)

      use ABC ();
      This also doesn't work. I still get errors.
      Too many arguments for ABC::new_time_var at /u/agarwsun/projects/packages/datelib/lib/perl5/5.8.6/i86pc-solaris/ABC.pm line 71, near "@_)"

      Not enough arguments for ABC::dl_ascii_to_time at /u/agarwsun/projects/packages/datelib/lib/perl5/5.8.6/i86pc-solaris/Deshaw/ABC.pm line 81, near "@_)"

      Not enough arguments for ABC::dl_compare at /u/agarwsun/projects/packages/datelib/lib/perl5/5.8.6/i86pc-solaris/Deshaw/ABC.pm line 91, near "@_)"

      The actual signatures of these subroutines are:
      <new_time_var>() <dl_ascii_to_date>(<string>) <dl_ascii_to_time>(<string>, <dtp>)

        Well, if the ABC::new_time_var function is declared with a prototype of (), meaning no arguments, you cannot call it with arguments in your wrapper, like ABC::new_time_var(@_).  Similarly for the other two functions.

        See also other threads on using prototypes here, to better understand how prototypes work in Perl (hint: they don't act like in C).