I have been writing a lot of scripts lately---things that I have always wanted to have but never had the time to think about. Now I have lots of time because maintaining my modules while in Iraq has been such a pain (going from a decent development process to this travesty is more than I can bear, and, having only about two months to my return, I have given up for the time being, having made way to many stupid mistakes).
Of course, I have the test bug, and because I know Andy Lester will make fun of me if I do not write tests for my code, I have been writing test suites for my scripts.
This creates a small problem: how do I test only parts of the script, like the subroutines, to make sure they are doing the right thing?
I could put all of the subroutines in a library or separate module, but then it turns into two (or more) files instead of one, and I do not want that for a script.
I could make the entire script a class, and add a run() method to the class, which is sort of the same thing that I see beginning C students do in their main().
int main ( ... ) { init(); call_this(); now_this(); clean_up(); }
% perl -MCPAN -e shell
(I notice that this problem has already been solved more or less nicely in Python and Java.)
if (!caller()) {
# ...
}
Re:caller?
bart on 2004-02-14T13:36:01
caller() is what I would use too. Just a bare:run() unless caller;Re:caller?
VSarkiss on 2004-02-14T16:05:15
Yup, that's exactly how
diagnostics.pm
becomessplain
, which is what I think brian is considering.Re:caller?
brian_d_foy on 2004-02-14T22:10:39
I was thinking about that too, but I just hadn't tried it. I have to sit at a desk for 12 hours tomorrow, so I should have plenty of time to figure it out. I did not know about splain, so I will have to see what diagnostics.pm does too.
Thanks to everyone for ideas.:) Re:caller?
Dom2 on 2004-02-15T10:05:32
This sounds very similiar to the python idiom:if __name__ == '__main__':
main()-Dom
package Class::Script;
use Filter::Simple;
FILTER { $_.= "\nrun(\@ARGV) unless caller;\n1;"; };
1;