|
Posted by Paul Lalli on October 16, 2006, 2:45 pm
Please log in for more thread options
Alf McLaughlin wrote:
> > Why are you doing this? What point do you think this has?
>
> I like to organize my scripts using a block like this. Then, I can
> localize variables to this block and call other sub routines from this
> block and return values to pass to other subroutines; it helps me keep
> track of what I'm doing. Is it bad? I am always looking for ways to
> improve my Perl scripts so please let me know. I don't actually think
> it's doing anything (I may as well call the block "FRAN" or "GREG").
That's actually exactly why it makes me do a double-take. By putting
it there, it's too easy for someone to be under the mistaken impression
that this block of code functions like the int main() function in C.
It doesn't. Limiting the scope of your variables is a good thing, and
I applaud you for making that decision. I'm just not sure labeling the
block MAIN: is the best of all choices.
> Your comments have actually been an immense help. I was really doing a
> lot of silly things, I can see, so I'll have to read up on Perl libraries.
FWIW, we generally call them "modules" in Perl, and you can read up on
them in:
perldoc perlmod
> For now, here is my final version of things that I think is a better
> way to do it:
>
> The library:
>
> package my_library;
>
> sub my_sub1 {
> return 'this is my_sub1';
> }
>
> sub my_sub2 {
> return 'this is my_sub2';
> }
>
> 1;
> __END__
>
> ###END of library.
>
> The script:
>
> #!/usr/bin/perl
>
> use my_library;
>
> MAIN: {
> my $foo = my_library::my_sub2();
> print "$foo\n";
> }
>
> ## END of script.
>
>
> I believe this style of writing libraries will solve my basic problem
> as opposed to what I was doing.
My only other suggestion is to make sure you add
use strict;
use warnings;
to *both* the module and the "main" file.
Paul Lalli
|