Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Self Testing Modules

by demerphq (Chancellor)
on Dec 18, 2005 at 00:21 UTC ( #517535=note: print w/replies, xml ) Need Help??


in reply to Self Testing Modules

The perl version of that is

unless (caller) { ... }

If you put a block like that in the main part (ie, not in a sub, preferably right before the true value that all modules end with) of a .pm file you can do

perl Module.pm

And it will execute, but if its loaded via use Module; it won't.

I think thats what you are after anyway.

You _could_ use if ($0 eq __FILE__) { ... } I suppose, but the problem there is that __FILE__ probably will be fully qualified, but $0 might not be.

Having said all of that, module test code in perl doesnt normally go in such a block. Its more for quick debug/test code that you use during development. If you want to test your code properly you should use the test framework that comes with perl.

Update: switched the 'if' to an 'unless' as per happy-the-monks correction.

---
$world=~s/war/peace/g

Replies are listed 'Best First'.
Re^2: Self Testing Modules
by Happy-the-monk (Canon) on Dec 18, 2005 at 01:00 UTC

    if (caller) { ... }

    All right except demerphq got it the wrong way round.
    What he meant was:

    unless ( caller ) { # here go the tests that are only called when the module is run as +: # perl Module.pm }

    It's part of my cargo cult programming style. You know the documentation of caller, as you've already written in this thread.

    Cheers, Sören

    Update: I just saw a similar thing on a thread at German language Perl-Community.de.
    eserte writes:

    return 1 if caller; # end of the module's own code # Test Code goes here

      Doh. Doh. Doh.

      happy-the-monk is correct, i got it backwards. :-(

      ---
      $world=~s/war/peace/g

Re^2: Self Testing Modules
by Sheol (Novice) on Dec 18, 2005 at 00:27 UTC

    Thank you for your prompt response. I think that is what I want.

    To learn more about this "caller" would I just be fine with a `perldoc -f caller` followed by a `perldoc -q caller` or is there another section of perldoc I should check as well or instead?


    I missed the second part

    As per using the building debuging suite, if I am thinking of what you are thinking of, that kind of won't really work. You're talking about `perl -c` and `perl -d` right? That really won't work in my case. Generally I've found those features really useful when the program is meant to run and stay running.

    However for some of what I plan on doing, will require a request response model. So the script will be run in many instances where it is easier just have trip alarms at certain points while working on it.

    This may sound like it contradicts what I am going for here, as to use caller, I'll need some fake data. However the module using it will be fairly simple. Only really done to keep things tidy and clean/easy to read for moshes.

    For what I am planning, I find it would be easier to do it this way, and I am still not a wizard with the debugging suite yet.

    However this train of thought brings up another thing I have been thinking over a few days.

      Happy-the-monk was correct, its actually unless(). And what i meant was to set up a proper test framework using h2xs, Test::More and Test::Harness. Then you can build a conventional Perl test framework, so eventually your module can be uploaded to CPAN (even if it never is, using the framework is still a big asset).

      ---
      $world=~s/war/peace/g

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://517535]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2021-06-24 11:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (127 votes). Check out past polls.

    Notices?