Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

warning about prototypes too early?

by jcpunk (Friar)
on Feb 23, 2004 at 17:48 UTC ( [id://331162]=perlquestion: print w/replies, xml ) Need Help??

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

I get the following warning when I run my script:
library::readConfig() called too early to check prototype at /home/httpd/cgi-bin/testing/rebuild/etc/custom_modules/library.pm line 35.
Anyone know how to avoid that kind of warning? I only ask because I was considering making a web based version of this script and don't want to fill up my apache logs with warnings. The sub in question is about 1000 lines of code away, but I can only move it a few hundred lines closer while maintaining company standards for code placement.

jcpunk
all code is tested, and doesn't work so there :p (varient on common PM sig for my own ammusment)

Replies are listed 'Best First'.
•Re: warning about prototypes too early?
by merlyn (Sage) on Feb 23, 2004 at 17:53 UTC
    Simple. Don't use prototypes. If your company policy requires you to use prototypes, have them give me a call at the office number listed on my home page, and I'll explain to them why they need to fix their policy.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Care to explain why they are there if they are evil? I sort of like having a decent check that the arguments to my functions are sound (most languages provide this), and have only adopted using them in Perl lately -- despite some controversy. Coming from a C/C++ background, forward declarations are considered a good thing.

      Is it true that in Perl a forward declaration is essentially a definition, and thus you run into problems with defining functions that never have a correctly defined body at a certain point in the code? That's the only reason I would think would make them a bad design choice.

      I will accept the wisdom of folks in this thread saying "don't do it", but I'm a stubborn beast and I usually need to know why I am or am not doing something.

        What about the performance increase you get from using prototypes? My understanding is that using prototypes gives you a circa 10% boost on performance. Is that not worth using them?

        hackmare.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: warning about prototypes too early?
by cees (Curate) on Feb 23, 2004 at 18:30 UTC

    If you are still keen on using prototypes after having read the above comments, I thought I would throw out the answer you were originally looking for.

    Throw in a 'use diagnostics;' at the top of your code, and you will get the following:

    (W prototype) You've called a function that has a prototype before the parser saw a definition or declaration for it, and Perl could not check that the call conforms to the prototype. You need to either add an early prototype declaration for the subroutine in question, or move the subroutine definition ahead of the call to get proper prototype checking. Alternatively, if you are certain that you're calling the function correctly, you may put an ampersand before the name to avoid the warning. See perlsub.

    This is telling you to put a forward declaration in your code that tells perl what the prototype of the function is, and stick it before the first use the function.

    sub readConfig ($$); # or whatever the prototype is

    Check out perlsub for more details.

    - Cees

Re: warning about prototypes too early?
by diotalevi (Canon) on Feb 23, 2004 at 17:56 UTC

    You've specified a prototype with your function. Don't. Perl's prototypes are not like C or C++ or Java. Its actually an entirely different sort of thing.

    You might also want to check out Devel::SummarizedWarnings if you're looking at too many identical errors.

Re: warning about prototypes too early?
by ambrus (Abbot) on Feb 23, 2004 at 21:19 UTC

    Insert a declaration for the sub somewhere before you call it. You can leave the definition of the sub where it is. A declaration in perl is like sub myfoo ($$);.

Re: warning about prototypes too early?
by hackmare (Pilgrim) on Feb 25, 2004 at 16:45 UTC

    When you use prototypes, perl wants you to declare the prototypes before you use them in the code.
    If you put your subs before the main body of your script, then this will fix the problem

    Shameless SVG plug: The March SVG London Users Meeting will take place Tuesday 9 March at the Round Table pub at 1900h.
    There will be two of perl-related technical presentations.


    hackmare.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-24 11:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found