Sub routine location

S

sunckell

hello everyone,

I have been working with perl a lot lately. Trying to read up as
much as I can. I am getting a little confused on how the script
"should look". I have downloaded and reviewed code from other
programmers, read several books, and I am seeing some discrepecies on
the location of sub routines.

My question: where should sub routines be located in a script?
Should they be at the top of the script, before MAIN, or at the end of
the script, after MAIN?

Thanks for the clarification....

SunCkell
 
P

Paul Lalli

sunckell said:
I have been working with perl a lot lately. Trying to read up as
much as I can. I am getting a little confused on how the script
"should look". I have downloaded and reviewed code from other
programmers, read several books, and I am seeing some discrepecies on
the location of sub routines.

My question: where should sub routines be located in a script?
Should they be at the top of the script, before MAIN, or at the end of
the script, after MAIN?

What the heck is MAIN?

This kind of structural appearance is largely the perview of the
individual programmer. My general preference is to declare the
subroutines near the top of the code, under the included modules. I
then write the base code, the part that controls the flow of the
program. Under the last executable code statement/block, I begin
defining my subroutines, in a roughly "most important -> least
important" order.

With very few exceptions, Perl couldn't care less where you define your
subroutines. Those exceptions are:
If you call a subroutine before declaring it, you must call it with
parentheses (ie, "foo('bar', 'baz');" rather than "foo 'bar', 'baz';"
If your subroutine uses prototypes, you must declare it before calling
it. ("must" in this case means "to avoid the warning and to actually
have the prototype enforced")

For further stylistic tips, see also:
perldoc perlstyle

Paul Lalli
 
S

sunckell

Thanks for the input...

As for "MAIN" if I make a syntax error in my subroutine somewhere, i
have seen errors that resemble..

error at some line number in &subroutine::main.

Or something similar. I am assuming main is section of script that
does the calling to subroutines etc. I guess I am wrong?

I thought I would ask this becuase I have noticed certian warnings (
I'll have to test this since I am remebering what I have seen) when
subroutines are at the bottom of the script and I call them just by
subroutine_name($var1,$var2) and not like
&subroutine_name($var1,$var2).

SunCkell
 
P

Paul Lalli

sunckell said:
Thanks for the input...

Who are you thanking? For what input? Please quote some context in
your replies. Please see the Posting Guidelines that are posted here
twice a week for more good advice.
As for "MAIN" if I make a syntax error in my subroutine somewhere, i
have seen errors that resemble..

error at some line number in &subroutine::main.

Or something similar. I am assuming main is section of script that
does the calling to subroutines etc. I guess I am wrong?

Difficult to know without a more precise example. That particular form
of an error message isn't jumping out at me as immediately familiar.
Perhaps you could contrive an example which generates this sort of
error message, and then copy and paste it directly?
I thought I would ask this becuase I have noticed certian warnings (
I'll have to test this since I am remebering what I have seen) when
subroutines are at the bottom of the script and I call them just by
subroutine_name($var1,$var2) and not like
&subroutine_name($var1,$var2).

My guess would be that this relates to one of the two "exceptions" I
noted in my first reply to your initial question - When your
subroutines are declared after being called, prototypes are not
enforced, and you get a warning telling you about it. When you call a
subroutine using the & in front of the name, that tells perl to disable
prototype checking. Perl assumes you are specifically asking for that
feature, and so doesn't bother warning you that it can't check the
prototypes in the first place.

A good rule of thumb is to never use & unless you know exactly what
reason you have to use it. 99% of the time, you don't have any reason
to.

Paul Lalli
 
B

Brian McCauley

sunckell said:
hello everyone,

I have been working with perl a lot lately. Trying to read up as
much as I can. I am getting a little confused on how the script
"should look". I have downloaded and reviewed code from other
programmers, read several books, and I am seeing some discrepecies on
the location of sub routines.

I, personally, come down on the side of putting the subroutines at the
top. This has the advantage that you can use the operator syntax for
calling subroutines.

I do however respect the right of others to put them at the bottom if
that's what they want to do.

I don't think that scattering them throughtout the main body is a very
good idea :)
 
G

Gunnar Hjalmarsson

Paul said:
My guess would be that this relates to one of the two "exceptions" I
noted in my first reply to your initial question - When your
subroutines are declared after being called, prototypes are not
enforced, and you get a warning telling you about it. When you call a
subroutine using the & in front of the name, that tells perl to disable
prototype checking. Perl assumes you are specifically asking for that
feature, and so doesn't bother warning you that it can't check the
prototypes in the first place.

A good rule of thumb is to never use & unless you know exactly what
reason you have to use it. 99% of the time, you don't have any reason
to.

In addition to that, don't use prototypes without a good reason. In
particular, don't make it a habit to declare subroutines like

sub myfunction() {

}

i.e. with parentheses after the subroutine name, but just do:

sub myfunction {

}
 
A

Anno Siegel

Brian McCauley said:
I, personally, come down on the side of putting the subroutines at the
top. This has the advantage that you can use the operator syntax for
calling subroutines.

I do however respect the right of others to put them at the bottom if
that's what they want to do.

I put them at the bottom for the same reason you put them on top: It
enforces parentheses with calls of locally defined subs, as opposed
to builtins and imported subs. I use operator syntax for the latter
if possible. I find the visual distinction useful, it is also in
accordance with PBP.

More generally, I try to define higher-level material before the lower-
level material that supports it. When reading code, I find that order
more natural than the other way around. Putting subs last is a consequence
of that.

Anno
 
C

ced

Anno said:
I put them at the bottom for the same reason you put them on top: It
enforces parentheses with calls of locally defined subs, as opposed
to builtins and imported subs. I use operator syntax for the latter
if possible. I find the visual distinction useful, it is also in
accordance with PBP.

More generally, I try to define higher-level material before the lower-
level material that supports it. When reading code, I find that order
more natural than the other way around. Putting subs last is a consequence
of that.

I've used both styles and never agreed with a rigid "either-or'
placement of
sub's. I like "higher level" first too but IMO, "higher-level" may be
nominal
only -- sometimes the most signficant activity occurs in sub's and the
code
may be easier to follow if they're upfront rather than buried near the
end.
 
G

Gunnar Hjalmarsson

Bob said:
Why?

I only program in perl occasionally but prefer to be as explicit as
possible. Using the () -- or whatever prototype is appropriate -- prevents
one from calling the function with the wrong arguments. Am I missing
something?

Probably not. My comment was in response to the OP's problem
description, which indicated that he might be using prototypes without
realizing it. I didn't intend to argue against the use of prototypes
when you know what you are doing. Sorry for not being clear.
 
X

xhoster

sunckell said:
hello everyone,

I have been working with perl a lot lately. Trying to read up as
much as I can. I am getting a little confused on how the script
"should look". I have downloaded and reviewed code from other
programmers, read several books, and I am seeing some discrepecies on
the location of sub routines.

My question: where should sub routines be located in a script?

I tend to put the subroutines that are the intellectual guts of the program
at the top (above the non-subroutine code) and the utility and scut-work
subroutines at the bottom.

Should they be at the top of the script, before MAIN, or at the end of
the script, after MAIN?

There is no Arizona. Wait, I mean there is no MAIN.

Xho
 

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

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top