http://qs321.pair.com?node_id=489107


in reply to testing in perl: main and caller

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.

Replies are listed 'Best First'.
Re^2: testing in perl: main and caller
by chromatic (Archbishop) on Sep 05, 2005 at 02:37 UTC

    That's basically what the book suggests. Thanks!

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

      ?

        The book has:

        ok( require( 'foo.pl' ), 'loaded file successfully' ) or exit;

        I think require_ok() would also work, though I'm not in a position to try it right now, nor do I remember if I wrote it as above for a specific reason.