Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: testing in perl: main and caller

by bmann (Priest)
on Sep 05, 2005 at 02:14 UTC ( #489107=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: testing in perl: main and caller
Select or Download Code
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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2015-07-03 03:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (47 votes), past polls