Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Order of subroutines
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

Report this thread to moderator Post Follow-up to this message
Old Post
Aris Xanthos
09-28-04 02:03 PM


Re: Order of subroutines
Aris Xanthos wrote:
> Hi,
>=20
> I have a problem with the order of subroutines
> and I couldn't find help about that in books or
> online.
>=20
> Could someone tell me why this code works fine:
>=20
> ------------------
>  #!/usr/bin/perl
>=20
>  Function1();
>=20
>  sub Function1() {
>    Function2( 'arg' );
>  }
>=20
>  sub Function2() {
>    my ( $arg ) =3D @_;
>  }
>=20
> ------------------
>=20
> and not this one:
>=20
> ------------------
>  #!/usr/bin/perl
>=20
>  Function1();
>=20
>  sub Function2() {
>    my ( $arg ) =3D @_;
>  }
>=20
>  sub Function1() {
>    Function2( 'arg' );
>  }
>=20
> ------------------
>=20
> which yields the following error message:
>=20
> Too many arguments for main::Function2 at=20
> D:\Developpement\Perl\test.pl line 10, near "'arg' )"
> Execution of D:\Developpement\Perl\test.pl=20
> 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=20
it assumes 1 argument is OK. In the second case, it can determine that=20
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=20
satisfied. If you change it to

sub Function2

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

HTH,

Josef

--=20
Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T.  Pratchett


Report this thread to moderator Post Follow-up to this message
Old Post
Josef Moellers
09-28-04 02:03 PM


Re: Order of subroutines
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

Report this thread to moderator Post Follow-up to this message
Old Post
Gunnar Hjalmarsson
09-28-04 02:03 PM


Re: Order of subroutines
"Aris Xanthos" <Aris.Xanthos@ling.unil.ch> wrote in message
news:b3b0e6f5.0409280258.51e7d232@posting.google.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 ) = @_;
>  }

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' );
>  }
>



Report this thread to moderator Post Follow-up to this message
Old Post
Paul Lalli
09-28-04 09:11 PM


Re: Order of subroutines
Actually a solution to my problem can be found in this post:

http://groups.google.ch/groups?selm...=gpl
ain

Sorry, I should have searched more.

AX

Report this thread to moderator Post Follow-up to this message
Old Post
Aris Xanthos
09-28-04 09:11 PM


Re: Order of subroutines
Aris.Xanthos@ling.unil.ch (Aris Xanthos) wrote in message news:<b3b0e6f5.0409280258.51e7d23
2@posting.google.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.

Report this thread to moderator Post Follow-up to this message
Old Post
Tony Skelding
09-28-04 09:11 PM


Re: Order of subroutines
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

Report this thread to moderator Post Follow-up to this message
Old Post
Andres Monroy-Hernandez
09-28-04 09:11 PM


Re: Order of subroutines
Thank you for your answers, all of them help.

Indeed I should have used strict and warnings.

Aris Xanthos

Report this thread to moderator Post Follow-up to this message
Old Post
Aris Xanthos
09-30-04 01:05 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Miscellaneous archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:30 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.