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

Prototype Mismatch Errors

by mavericknik (Sexton)
on Aug 07, 2018 at 19:07 UTC ( [id://1220028]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I'm changing a large file coded by someone else and am getting prototype errors. Snippet:
use Getopt::Long; use Data::Dumper; use List::Util qw[min max]; use POSIX; use Cwd 'abs_path'; use Date::Parse; use Date::Format; print "Hello World\n";
Gives:
Prototype mismatch: sub main::strftime: none vs ($\@;$) at /pkgs/perl/ +5.14.1/lib64/5.14.1/Exporter.pm line 67. at ./trial.pl line 9 Prototype mismatch: sub main::ctime: none vs ($;$) at /pkgs/perl/5.14. +1/lib64/5.14.1/Exporter.pm line 67. at ./trial.pl line 9 Prototype mismatch: sub main::asctime: none vs (\@;$) at /pkgs/perl/5. +14.1/lib64/5.14.1/Exporter.pm line 67. at ./trial.pl line 9 Hello World
I understand this is because these functions are begin declared multiple times? What is the best way to fix these warnings without having to go through the whole code? Is it even possible? Thanks!

Replies are listed 'Best First'.
Re: Prototype Mismatch Errors
by LanX (Saint) on Aug 07, 2018 at 20:10 UTC
    > What is the best way to fix these warnings without having to go through the whole code?

    Looks like two modules are exporting these equally named functions by default, you should definitely decide which one you prefer and block the others!

    Date::Format is one of them, POSIX the other one.

    > Is it even possible?

    Depends on the modules, look into the documentation, normally you say something like use Module qw(func1 func2); to restrict your selection. See use ...

    HTH!

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      mavericknik: I completely agree with LanX's advice. Depending on whether you want POSIX::strftime or Date::Format::strftime, you could either:

      use Date::Format qw/time2str ctime asctime/; # everything except strf +time

      (See the source for the default @EXPORT list.) And from POSIX:

      Everything is exported by default (with a handful of exceptions). This is an unfortunate backwards compatibility feature and its use is strongly discouraged. You should either prevent the exporting (by saying use POSIX ();, as usual) and then use fully qualified names (e.g. POSIX::SEEK_END), or give an explicit import list. If you do neither and opt for the default (as in use POSIX;), you will import hundreds and hundreds of symbols into your namespace.

      So following this advice from the documentation is something I'd recommend regardless.

      Update: Clarified last sentence.

Re: Prototype Mismatch Errors
by talexb (Chancellor) on Aug 07, 2018 at 22:49 UTC

    If you're using two modules, both of which export subs with the same names, you need to decide which of the module's exports you plan to use.

    You might start by commenting out all of the modules, compiling the script, and seeing where the errors pop up. Add a module back in as necessary, and compile again. You might not be using some of the modules, so solving the issue you've found might not even be required.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Re: Prototype Mismatch Errors
by LanX (Saint) on Aug 07, 2018 at 20:41 UTC
    On another note, don't you activate warnings ?

    I'm surprised you're not seeing any "subroutine redefined" errors.

    (can't test ATM)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      $ cat use.pl #!/usr/bin/env perl use strict; use warnings; use POSIX; use Date::Format; $ ./use.pl Prototype mismatch: sub main::strftime: none vs ($\@;$) at /usr/share/ +perl5/vendor_perl/Exporter.pm line 66. at ./use.pl line 5. Prototype mismatch: sub main::ctime: none vs ($;$) at /usr/share/perl5 +/vendor_perl/Exporter.pm line 66. at ./use.pl line 5. Prototype mismatch: sub main::asctime: none vs (\@;$) at /usr/share/pe +rl5/vendor_perl/Exporter.pm line 66. at ./use.pl line 5. $ perl -v | head -3 This is perl 5, version 20, subversion 3 (v5.20.3) built for x86_64-li +nux-thread-multi (with 16 registered patches, see perl -V for more detail)

      I would hypothesise that because the prototypes are different, the subroutines are not actually redefined. Just a guess, though.

Re: Prototype Mismatch Errors
by kcott (Archbishop) on Aug 08, 2018 at 11:44 UTC

    G'day mavericknik,

    You can be very selective about what you import. See Exporter: Specialised Import Lists for full details. Here's a quick example:

    $ perl -E 'use Data::Dump; my $x = {qw{a 1 b 2}}; dd $x' { a => 1, b => 2 } $ perl -E 'use Data::Dump qw{!dd}; my $x = {qw{a 1 b 2}}; dd $x' Can't call method "dd" on unblessed reference at -e line 1. $

    — Ken

Re: Prototype Mismatch Errors
by AnomalousMonk (Archbishop) on Aug 07, 2018 at 20:11 UTC

    Update: LanX's seems | is the better answer. Don't bother with the rest of this.

    I understand this is because these functions are begin declared multiple times?

    c:\@Work\Perl\monks>perl -wMstrict -le "BEGIN { sub Dumper (); } ;; use Data::Dumper; " Prototype mismatch: sub main::Dumper () vs none at C:/Perl/lib/Exporte +r.pm line 67. at -e line 1 c:\@Work\Perl\monks>perl -wMstrict -le "BEGIN { sub Dumper; } ;; use Data::Dumper; "
    From this, it seems you may be right. But that prompts the question, "Why are you 'begin declaring' subroutines at all?" Can you show a Short, Self-Contained, Correct Example of such declarations that represents what is in your code (and produces the same error message)?


    Give a man a fish:  <%-{-{-{-<

      AnomalousMonk, excepting line numbers, the OP code does produce those messages for me. As LanX said, it's a conflict on importing the same subroutines from POSIX and Date::Format. This can be seen with perl -MPOSIX -MDate::Format -e 1, which also produces those messages

      (edit: add links)

      (edit 2: sorry, you updated after I started this reply...)

        ... OP code does produce those messages ...

        I belatedly noticed the OPed code was, in fact, an SSCCE. I think it was the word "snippet" that got me onto the wrong track. :)


        Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 09:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found