Order of subroutines

A

Aris Xanthos

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
 
J

Josef Moellers

Aris said:
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
 
G

Gunnar Hjalmarsson

Aris said:
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:

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.
 
P

Paul Lalli

Aris Xanthos said:
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
 
T

Tony Skelding

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.
 
A

Andres Monroy-Hernandez

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.
 
A

Aris Xanthos

Thank you for your answers, all of them help.

Indeed I should have used strict and warnings.

Aris Xanthos
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top