Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • Not sure what you're doing there. The require is likely loading the program into the main namespace, just like you already are. The import is importing against main, from main. I bet if you remove it, it works exactly the same.
    • Randal L. Schwartz
    • Stonehenge
  • Let's say you have this


    sub do_something {
        return 42;

    print "Running\n";

    and this


    require '';
    print "ok 1\n" if do_something()==42;

    Now, running produces:

    ok 1

    because the require will execute the program. So, if the program dies because @ARGV is wrong, or somesuch, your test dies as well...

  • If at the top level, you check caller(), you'll find it is false if the script is run as a standalone program, and true if it's loaded as a library.

    Thus, do this at the top level:

    unless(caller) {
       # former top level
       print "Run as a script\n";
    } else {
       # you may drop this if you don't need it
       print "Initializing as a library file\n";
    It'll work now.
    • Thank you. Sounds like this advice was either present in the slides I read, and I missed it (it was middle of the night, up late hacking for work :) ), or else the presenter missed it or only mentioned it orally without including it in the slides.

      J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
  • Besides your update, import() is a method. When you call methods as functions in Perl, you don't always get what you want.

    I think you'll have more luck checking caller() to see if someone has invoked your program directly.

  • You write your programs as modules, where the main script is actually wrapped in a subroutine (just like the good old days of int main(void)). Now a program is just a set of subroutines and you can use it like every other library of subroutines (including testing it).

    I write about it in Chapter 18 of Mastering Perl []
  • Hopefully it wasn't my slides [] that were way off ;-) Here's how I poked at the internals of a program, including global variables. I got the idea from another post here on use Perl, and I hacked out this working program and test.

    phred@pooky ~ $ cat
    #!perl -w

    use strict;
    use warnings;

    our $var = 0;

    sub addition {
        my ($one, $other_one) = @_;
        my $result = $one + $other_one+$var;
        return $result;

    sub should_die {
        die "oops";


    phred@pooky ~ $ cat

    • Oops, I forgot the output

      phred@pooky ~ $ perl foo.t
      ok 1 - require '';
      ok 2 - 1+1+var returns 7
      ok 3 - should_die() died