Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Order of subroutines

Reply
Thread Tools

Order of subroutines

 
 
Aris Xanthos
Guest
Posts: n/a
 
      09-28-2004
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
 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      09-28-2004
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

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      09-28-2004
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
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      09-28-2004
"Aris Xanthos" <> wrote in message
news: om...
> 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' );
> }
>



 
Reply With Quote
 
Aris Xanthos
Guest
Posts: n/a
 
      09-28-2004
Actually a solution to my problem can be found in this post:

http://groups.google.ch/groups?selm=...&output=gplain

Sorry, I should have searched more.

AX
 
Reply With Quote
 
Tony Skelding
Guest
Posts: n/a
 
      09-28-2004
(Aris Xanthos) wrote in message news:<. com>...
> 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.
 
Reply With Quote
 
Andres Monroy-Hernandez
Guest
Posts: n/a
 
      09-28-2004
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
 
Reply With Quote
 
Aris Xanthos
Guest
Posts: n/a
 
      09-29-2004
Thank you for your answers, all of them help.

Indeed I should have used strict and warnings.

Aris Xanthos
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
defining subroutines when using interpreter interactively? MackS Perl 0 03-11-2005 01:26 AM
Global subroutines fd123456 ASP .Net 6 02-04-2005 11:12 PM
Global subroutines fd123456 ASP .Net 1 01-28-2005 11:12 PM
Global subroutines tshad ASP .Net 7 01-26-2005 06:42 PM
References and subroutines ReaprZero Perl 1 12-04-2003 02:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57