Perl: the Markov chain saw | |
PerlMonks |
Re: Using guards for script execution?by Marshall (Canon) |
on Mar 01, 2017 at 05:05 UTC ( [id://1183241]=note: print w/replies, xml ) | Need Help?? |
A few comments to your Perl code,
This code is a weird formulation because the file that contains main() will always be executed as a main program? Normal practice would be to do away with this extra level of indentation implied by the subroutine main and just start writing the "main" code. The unless (caller){} does nothing useful. You could have just put a simple main(); instead to call the sub main.
I attach demomain.pl and demo.pm below. In my demo.pm file, I use caller(), test() if !caller;. If demo.pm is being run as a main program, test() will execute. If demo.pm is being "used" by another program, say by demomain.pl, all of the top-level code in demo.pm will run, but test() will not run because Perl knows that demo.pm is not being run as a main program. There is no need to so something similar in your main.pl program because your main is always a main program! This provides a very "lightweight" test framework. There are such things as .t files which are used in more complicated situations. It is possible to split a main program across multiple files, i.e., same package in multiple files. I don't demo that because I think it is a very bad idea.
In Section
Seekers of Perl Wisdom
|
|