Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: How Do I Bundle Perl Modules and Their Tests Into the Same File?

by jdhedden (Deacon)
on Feb 13, 2006 at 00:16 UTC ( #529710=note: print w/replies, xml ) Need Help??


in reply to How Do I Bundle Perl Modules and Their Tests Into the Same File?

The ifdef module would be useful for this. Write your module like this:
package MyClass; { # Put class code here inside the braces to # constrain the lexical scope of its variables ... } 1; =begin TEST package main; # Test code my $obj = MyClass->new(); ... =cut
Then run it like this:
perl -Mifdef=TEST MyClass.pm
I'm sure others will offer more ideas, too.

Update: Added missing 1; as pointed out below.


Remember: There's always one more bug.

Replies are listed 'Best First'.
Re^2: How Do I Bundle Perl Modules and Their Tests Into the Same File?
by perlknight (Pilgrim) on Feb 13, 2006 at 00:30 UTC
    hmm, a module need to have "1;" as the last entry. Where does this goes in this case? The last line or in the module it is declaring, e.g for this particalur example in the MyClass package?
      Before of after the =begin..=cut, it doesn't matter. When used as a module, =begin..=cut is just a comment, so it's ignored. When used as a script (to test the module), the 1; isn't needed since it's not a module.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2023-06-01 12:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?