Order of subroutines

Discussion in 'Perl Misc' started by Aris Xanthos, Sep 28, 2004.

  1. Aris Xanthos

    Aris Xanthos Guest

    Hi,

    I have a problem with the order of subroutines
    and I couldn't find help about that in books or
    online.

    Could someone tell me why this code works fine:

    ------------------
    #!/usr/bin/perl

    Function1();

    sub Function1() {
    Function2( 'arg' );
    }

    sub Function2() {
    my ( $arg ) = @_;
    }

    ------------------

    and not this one:

    ------------------
    #!/usr/bin/perl

    Function1();

    sub Function2() {
    my ( $arg ) = @_;
    }

    sub Function1() {
    Function2( 'arg' );
    }

    ------------------

    which yields the following error message:

    Too many arguments for main::Function2 at
    D:\Developpement\Perl\test.pl line 10, near "'arg' )"
    Execution of D:\Developpement\Perl\test.pl
    aborted due to compilation errors.

    I have this problem in the framework of a rather
    long (tk) script (about 2'000 lines so far), and the
    calls to various subroutines are getting too intricate
    for me to find an order that works (besides, it is not
    a satisfying solution).

    I hope this does not duplicate a previous post.

    Aris Xanthos
     
    Aris Xanthos, Sep 28, 2004
    #1
    1. Advertising

  2. Aris Xanthos wrote:
    > Hi,
    >
    > I have a problem with the order of subroutines
    > and I couldn't find help about that in books or
    > online.
    >
    > Could someone tell me why this code works fine:
    >
    > ------------------
    > #!/usr/bin/perl
    >
    > Function1();
    >
    > sub Function1() {
    > Function2( 'arg' );
    > }
    >
    > sub Function2() {
    > my ( $arg ) = @_;
    > }
    >
    > ------------------
    >
    > and not this one:
    >
    > ------------------
    > #!/usr/bin/perl
    >
    > Function1();
    >
    > sub Function2() {
    > my ( $arg ) = @_;
    > }
    >
    > sub Function1() {
    > Function2( 'arg' );
    > }
    >
    > ------------------
    >
    > which yields the following error message:
    >
    > Too many arguments for main::Function2 at
    > D:\Developpement\Perl\test.pl line 10, near "'arg' )"
    > Execution of D:\Developpement\Perl\test.pl
    > aborted due to compilation errors.


    It's as it says:
    In the first case, the compiler cannot know how much arguments Function2
    accepts, because the prototype of Function2 happens after the call, so
    it assumes 1 argument is OK. In the second case, it can determine that
    Functon2 does not accept any arguments, so it complains.
    If you change the definition of Function 2 to

    sub Function2($)

    the compiler will know that it accepts a single argument and will be
    satisfied. If you change it to

    sub Function2

    the compiler will know that you don't care and will be satisfied.

    HTH,

    Josef

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Sep 28, 2004
    #2
    1. Advertising

  3. Aris Xanthos wrote:
    > I have a problem with the order of subroutines
    > and I couldn't find help about that in books or
    > online.
    >
    > Could someone tell me why this code works fine:
    >
    > ------------------
    > #!/usr/bin/perl


    use strict;
    use warnings;

    and let Perl hint you about possible problems.

    > Function1();
    >
    > sub Function1() {

    ----------------^^

    Why are you using prototypes? If you don't know, you shouldn't do that.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 28, 2004
    #3
  4. Aris Xanthos

    Paul Lalli Guest

    "Aris Xanthos" <> wrote in message
    news:...
    > Hi,
    >
    > I have a problem with the order of subroutines
    > and I couldn't find help about that in books or
    > online.
    >
    > Could someone tell me why this code works fine:
    >
    > ------------------
    > #!/usr/bin/perl
    >
    > Function1();
    >
    > sub Function1() {
    > Function2( 'arg' );
    > }
    >
    > sub Function2() {
    > my ( $arg ) = @_;
    > }


    If you had warnings enabled, you would see that while this code 'works',
    it certainly doesn't work 'fine'. Indeed, that warning message would
    help you to understand why the second snippet doesn't work at all.

    Please ask the Perl interpreter for help before asking thousands of
    people on the internet for help.

    Paul Lalli

    > and not this one:
    > ------------------
    > #!/usr/bin/perl
    >
    > Function1();
    >
    > sub Function2() {
    > my ( $arg ) = @_;
    > }
    >
    > sub Function1() {
    > Function2( 'arg' );
    > }
    >
     
    Paul Lalli, Sep 28, 2004
    #4
  5. Aris Xanthos

    Aris Xanthos Guest

    Aris Xanthos, Sep 28, 2004
    #5
  6. (Aris Xanthos) wrote in message news:<>...
    > Hi,
    >
    > I have a problem with the order of subroutines
    > and I couldn't find help about that in books or
    > online.
    >
    > Could someone tell me why this code works fine:
    >
    > ------------------
    > #!/usr/bin/perl
    >
    > Function1();
    >
    > sub Function1() {
    > Function2( 'arg' );
    > }
    >
    > sub Function2() {
    > my ( $arg ) = @_;
    > }
    >
    > ------------------
    >
    > and not this one:
    >
    > ------------------
    > #!/usr/bin/perl
    >
    > Function1();
    >
    > sub Function2() {
    > my ( $arg ) = @_;
    > }
    >
    > sub Function1() {
    > Function2( 'arg' );
    > }
    >
    > ------------------
    >
    > which yields the following error message:
    >
    > Too many arguments for main::Function2 at
    > D:\Developpement\Perl\test.pl line 10, near "'arg' )"
    > Execution of D:\Developpement\Perl\test.pl
    > aborted due to compilation errors.
    >
    > I have this problem in the framework of a rather
    > long (tk) script (about 2'000 lines so far), and the
    > calls to various subroutines are getting too intricate
    > for me to find an order that works (besides, it is not
    > a satisfying solution).
    >
    > I hope this does not duplicate a previous post.
    >
    > Aris Xanthos


    You probably don't realise that you are defining a prototype for
    Function2, i.e.

    sub Function2() {
    ....

    says that the function should expect no parameters. Leave the () off
    and it will be unprototyped.

    The reason the first example works OK is that the prototype for
    Function2 has not been compiled at the time that Function1 is
    compiled.
     
    Tony Skelding, Sep 28, 2004
    #6
  7. Try not using empty prototypes. Your code would be cleaner if you had:

    Function1();
    sub Function2 {
    my ( $arg ) = @_;
    }
    sub Function1 {
    Function2('arg');
    }

    It seems that there is no need for prototypes.

    ---
    -Andres Monroy-Hernandez
     
    Andres Monroy-Hernandez, Sep 28, 2004
    #7
  8. Aris Xanthos

    Aris Xanthos Guest

    Thank you for your answers, all of them help.

    Indeed I should have used strict and warnings.

    Aris Xanthos
     
    Aris Xanthos, Sep 29, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ReaprZero

    References and subroutines

    ReaprZero, Dec 4, 2003, in forum: Perl
    Replies:
    1
    Views:
    455
    Gunnar Hjalmarsson
    Dec 4, 2003
  2. MackS
    Replies:
    0
    Views:
    474
    MackS
    Mar 11, 2005
  3. tshad

    Global subroutines

    tshad, Jan 24, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    379
    IPGrunt
    Jan 26, 2005
  4. fd123456

    Global subroutines

    fd123456, Jan 25, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    406
    tshad
    Feb 4, 2005
  5. fd123456

    Global subroutines

    fd123456, Jan 28, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    402
    tshad
    Jan 28, 2005
Loading...

Share This Page