perl -c manages because the Perl interpreter itself is getting control before execution starts at all and then just exiting.

You can use the debugger hooks to do this if you don't want any of the code in monster.pl to execute at all.

If you create a sub called DB::DB, Perl will enter it just as soon as the code has compiled but before the first statement executes. You can then do what you like with the subs back in main:: (the namespace of the monster.pl script, assuming it doesn't have internal packages).

Try something like this: in a new file, Devel::Stop:

package Devel::Stop; use strict; use warnings; sub DB::DB { # Execute the code you want to run here my $result = main::foo(); print STDERR "1..2\n"; print STDERR ((defined $result ? "ok 1" : "not ok 1")," - got a valu +e\n"); print STDERR (($result eq "I am foo" ? "ok 2" : "not ok 2"), " - rig +ht value\n"); exit(0); } 1;
and you'll get
joe-desk-> perl -d:Stop xxx 1..2 ok 1 - got a value ok 2 - right value
DB::DB gets called as soon as the code is loaded and compiled (including use statements in the main program). Since it calls exit before it returns, control never returns to the program being "debugged".

Run this like this:

perl -d:Stop monster.pl
Exercise for the reader to make the mechanism more flexible so you don't have to write a new package for every test, though a new package for every test is better than no tests at all...

Note that Test::Simple won't work here: it runs stuff in BEGIN and END blocks, and confuses this ultra-simple debugger. I'd recommend either writing your own little library to handle the ok, like, etc. functions, or beefing up the logic in DB::DB to allow the Test:: code to run but not anything in main::.

This is meant to demonstrate a direction you can go rather than be a complete solution; there's a lot of potential here, accompanying a lot of potential "why did THAT happen?" situations.


In reply to Re: How to load the subs without running the script by pemungkah
in thread SOLVED: How to load the subs without running the script by Narveson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.