Perl how to organize code between subroutines and main script
I have one perl script that has a main script and a couple of routines interleaved together. It looks like this:
sub utils1 {
...
}
# some code
# some more code that calls utils1
sub utils2 {
...
}
# some code that calls utils2
sub utils3 {
...
}
# some code that calls utils3
# the rest of code
Is there a better way to organize your code? Look for a good coding convention. Based on my python coding experience, I am thinking of something like below. What does it look like?
sub utils1 {
...
}
sub utils2 {
...
}
sub utils3 {
...
}
sub main {
# some code
# some more code that calls utils1
# some code that calls utils2
# some code that calls utils3
# the rest of code
}
&main();
+3
source to share
1 answer
As far as I know, there is no good practice defined for organizing routines. In my experience, documentation often dictates the order.
eg.
use Getopt::Lucid;
=head1 SYNOPSIS
This program does nothing so long ...
=cut
# ... main code here, not necessary to wrap into a sub
=head1 PUBLIC METHODS
=cut
sub method1 {
=head2 method1
This method does something ...
=cut
$self = shift;
# ...
}
sub method2 {
=head2 method2
This method does something different...
=cut
$self = shift;
# ...
}
=head1 PRIVATE METHODS
These methods are private their interface may change.
=cut
sub _priv1 {
=head2 _priv1
The _priv1 method is for ... and used by ....
=cut
my $self = shift;
# ...
}
+2
source to share