tags:

views:

140

answers:

1

In this earlier Stackoverflow question and especially brian d foy's "How a Script Becomes a Module" I've read about how to set up code so that it can be run as either a script or a module, using this technique:

package SomeModule;

__PACKAGE__->run(@ARGV) unless caller();

sub run {
    # Do stuff here if you are running the file as
    # a script rather than a module.
}

What is the purpose of __PACKAGE__ in this setup? Why not just do this?

run(@ARGV) unless caller();
+9  A: 

If you say __PACKAGE__->run(@ARGV) then run can be defined in a class you inherit from or allows a class to inherit from this one. If you just say run(@ARGV) you are missing the class information. This only matters if you are doing OO style programming.

Put the following in a file named Foo.pm, and then say perl Foo.pm 1 2 3

package Foo;

use strict;
use warnings;

__PACKAGE__->main(@ARGV) unless caller;

sub main {
    my $class = shift;
    my $obj   = $class->new(@ARGV);
    print $obj->to_string, "\n";
}

sub new {
    my $class = shift;
    return bless [@_], $class;
}

sub to_string {
    my $self = shift;
    return "args: " . join ", ", map { "[$_]" } @$self;
}

1;

Now put the following in Bar.pm, and say perl Bar.pm a b c.

package Bar;

use strict;
use warnings;

use base 'Foo';

__PACKAGE__->main(@ARGV) unless caller;

sub to_string {
    my $self = shift;
    return "args: " . join ", ", map { "<$_>" } @$self;
}

1;

Now lets see what happens if you don't use __PACKAGE__ in this environment. Make the first section of code in Foo.pm look like this:

main(@ARGV) unless caller;

sub main {
    my $obj = Foo->new(@ARGV);
    print $obj->to_string, "\n";
}

Now run perl Foo.pm 1 2 3. Everything should still look right. Now try running perl Bar.pm a b c. We still get output, but it is not the output we expect. What happens if we remove __PACKAGE__ from Bar.pm? Well, we get and error: Undefined subroutine &Bar::main called at Bar.pm line 8. This is because there is no main function in the module and we are not calling it with the class, so it won't look for it in the Foo package anymore.

Chas. Owens