Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

testing in perl: main and caller

by water (Deacon)
on Sep 05, 2005 at 01:16 UTC ( [id://489105]=perlquestion: print w/replies, xml ) Need Help??

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

Hi --

chromatic's & ian langworth's most excellent book on perl testing had a tip on how to use caller and a subroutine do_main so that a perl script ("perl foo.pl") could also be loaded in a test script (eq 'use'd or 'require'd) w/o executing, so its subs could be tested.

sadly i lent the book away and won't have access to it for a while.

could someone assist with that idiom?

thanks!

water

Replies are listed 'Best First'.
Re: testing in perl: main and caller
by bmann (Priest) on Sep 05, 2005 at 02:14 UTC
    I'm not sure if this is what chromatic suggested, but either one of these should do what you're looking for:
    unless ( defined caller ) { #code here }
    or
    sub do_me { #code to run here } do_me unless defined caller;
    sub do_me or the block after unless won't be executed when used or required, but will be executed from the command line.

    From perldoc caller:

    Returns the context of the current subroutine call. In scalar context, returns the caller's package name if there is a caller, that is, if we're in a subroutine or eval or require, and the undefined value otherwise.

      That's basically what the book suggests. Thanks!

        what does the test code look like, then?
        use_ok('foo.pl');
        isn't correct, clearly.

        ?

Re: testing in perl: main and caller
by xdg (Monsignor) on Sep 06, 2005 at 11:40 UTC

    For more on this technique, check out brian d foy's article 'How a script becomes a module'

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-25 21:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found