Perl -d and modulino issue
Asked Answered
A

1

8

I have some scripts that I have started unit-testing using the "modulino" idea. I have encountered a problem in that when the script is called with "perl -d" the script does not run as caller() returns a true value.

I have the main body of the script wrapped in a main() and some subroutines being slowly pulled out of the main() into their own subroutines.

At the top of the script I have:

main(@ARGS) unless caller();

When called in .t tests it works as I want, not running main() so I can test the subroutines. When I call the script from CLI it works great calling main().

The problem occurs when I call it from the CLI with:

perl -d myscript.pl

At this stage caller returns a valid value (rather than undef) and main is not called.

Suggestions would be much appreciated about how to approach this one.

Agamemnon answered 20/2, 2012 at 17:12 Comment(3)
Good catch. I'd never consider that since I hardly ever use the perl debugger. Thanks, :)Charente
I can't seem to reproduce this behavior. I wrote a tiny modulino, and the debugger still works fine. Can you post an example?Hedvig
There should be @ARGV instead of @ARGS. In case someone was just copy-pasting :)Lorenza
L
9

The situation with -d switch is similar as with testing - your code is executed by something else, in this case the debugger.

You can either run main yourself by calling it in the debugger manually or you have to detect if caller is debugger. Something like:

main(@ARGS) if !caller() || (caller)[0] eq 'DB';
Leaguer answered 20/2, 2012 at 17:31 Comment(2)
Your condition is wrong. If caller is true, then the second condition never gets evaluated. Try unless caller and (caller)[0] ne 'DB'.Hedvig
@Hedvig - thanks, fixed. I turned condition to positive as it took me quite some time to figure it out.Leaguer

© 2022 - 2024 — McMap. All rights reserved.