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

by perlknight (Pilgrim)
 on Feb 12, 2006 at 23:37 UTC Need Help??

perlknight has asked for the wisdom of the Perl Monks concerning the following question:

All, in python you can write a module and declare package main to test it. Can something like this be done in perl? e.g:
package abc;
come code...
package main
some test code for this module
[download]
Thanks.

2006-02-13 Retitled by Steve_p, as per Monastery guidelines
Original title: 'Can this be done...'

Replies are listed 'Best First'.
Re: How Do I Bundle Perl Modules and Their Tests Into the Same File?
by BrowserUk (Patriarch) on Feb 13, 2006 at 00:39 UTC

package abc;
...
return 1 if caller;
package main;

my $abc = abc->new(); ... [download] To run the tests, use perl \path\to\lib\abc.pm [download] Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error. Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal? "Science is about questioning the status quo. Questioning authority". In the absence of evidence, opinion is indistinguishable from prejudice. would this work if the module is required, from a sub? for instance: sub foo { require abc; abc->bar(); } [download] Yes. Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error. Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal? "Science is about questioning the status quo. Questioning authority". In the absence of evidence, opinion is indistinguishable from prejudice. 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 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
[download]
Then run it like this:
perl -Mifdef=TEST MyClass.pm
[download]
I'm sure others will offer more ideas, too.

Update: Added missing 1; as pointed out below.

Remember: There's always one more bug.
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.
Re: How Do I Bundle Perl Modules and Their Tests Into the Same File?
by bmann (Priest) on Feb 13, 2006 at 04:38 UTC
Try this:

package abc;
# ...

package main;
sub test_me {
# test code here
}

test_me unless defined caller;
[download]
Like python, if the module is used or required test_me will not be executed. If you run the module using perl Module.pm, the code in test_me will be executed. Update: Just realized BrowserUK posted a very similar solution above...

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://529706]
Approved by spiritway
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2023-03-28 05:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Which type of climate do you prefer to live in?

Results (66 votes). Check out past polls.

Notices?