Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

why does location of function matter?

by smartyollie (Novice)
on Jun 01, 2015 at 21:54 UTC ( [id://1128656]=perlquestion: print w/replies, xml ) Need Help??

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

I have run into a situation several times at my current job where the location of a perl function affects whether it works or not. I've never had this happen before and feel like I'm missing something obvious. The current scenario happens on both Windows (Strawberry Perl) and UNIX (Solaris v5.8.4). I have a function called PostValidate with one argument. When I put the function definition AFTER the main body of code, it runs; when I put the function before, it fails with

Too many arguments for main::PostValidate at y.pl line 349, near "$ofile)"

The call within the main body looks like this:

if ($validate) {
PostValidate($ofile);
}

I don't have any prototypes defined and I'm really at a loss as to why it should matter where the function is. Any ideas?

Replies are listed 'Best First'.
Re: why does location of function matter?
by BrowserUk (Patriarch) on Jun 01, 2015 at 22:09 UTC
    I don't have any prototypes defined and I'm really at a loss as to why it should matter where the function is. Any ideas?

    You don't show us the critical piece of information, namely the function definition; but from the error message, I'd guess that your function looks something like this:

    sub PostValidate() { #...............^^ # stuff here. ... }

    Which defines a function that takes no arguments.

    If that is seen before the function is called; it will complain that you passed too many arguments.

    If it is not seen until after the function call is compiled, then the prototype is too late to be applied.

    Does that fit your code?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

      With warnings enabled that would cause a "X called too early to check prototype" warning.

      ##########################
      sub PostValidate() {
      ##########################
      my ($file) = @_;
      my $cmd = "wc -l $file";
      {more stuff}

        That should be written as:

        sub PostValidate { ... }

        As you have it declared, sub PostValidate does declare a prototype. An empty prototype means no args may be passed (assuming Perl sees the subroutine is declared before its first use, as you've discovered).


        Dave

        I don't have any prototypes defined

        sub PostValidate() {

        An empty prototype () is still a prototype.

        You should always Use strict and warnings.

Re: why does location of function matter?
by AnomalousMonk (Archbishop) on Jun 02, 2015 at 02:52 UTC
Re: why does location of function matter?
by GotToBTru (Prior) on Jun 02, 2015 at 14:42 UTC

    "I don't have any prototypes defined" and yet the error says Perl thinks you do. In the eternal battle between what the programmer meant and what the compiler interprets, there can be only one winner ;)

    Dum Spiro Spero
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: why does location of function matter?
by GotToBTru (Prior) on Jun 01, 2015 at 22:05 UTC

    The explanation would be easier if we saw your code.

    Dum Spiro Spero
Re: why does location of function matter?
by Anonymous Monk on Jun 01, 2015 at 22:09 UTC

    Can't reproduce. Could you provide a small piece of working example code that demonstrates the problem?

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-18 03:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found