Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: How to export subs in a bin/script for testing in t/00.t

by ikegami (Pope)
on Feb 26, 2010 at 15:54 UTC ( #825542=note: print w/replies, xml ) Need Help??


in reply to How to export subs in a bin/script for testing in t/00.t

You can use caller to check if a script was loaded as a module.
  • Comment on Re: How to export subs in a bin/script for testing in t/00.t

Replies are listed 'Best First'.
Re^2: How to export subs in a bin/script for testing in t/00.t
by leocharre (Priest) on Feb 26, 2010 at 19:18 UTC
    Oooh! Very cool!..

    I wrapped the part of the main body that runs stuff into a sub run() and then I have a line such as (in bin/script).. (pseudocode follows..)

    #!/usr/bin/perl caller() or run(); sub run { print do_stuff_1(); print do_stuff_2(); } sub do_stuff_1 { 1 } sub do_stuff_2 { 2 } 1;
    Then in t/00.t
    use Test::Simple 'no_plan'; require 'bin/script'; ok( do_stuff_1(), 'do_stuff_1()' );
    Thank you! This is a wonderful solution. (Maybe someone has another (more elegant, less intrusive?) way to do this?)
      Maybe not more elegant but the name "main" instead of "run" would be a more common convention. Actually google found this exact example here, putting a package name at the top, and calling
      __PACKAGE__->main() unless caller();

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://825542]
help
Chatterbox?
[Eily]: I'm programming in python
[Eily]: I think I'm doing something like 3-4 google searches for each line that I write
[Eily]: I feel so efficient :D
[Eily]: (and it's not perl, where one line can do more than one thing at once...)

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2018-05-22 15:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?