When setting up Perl code to run as either a script or module, what is the reason for __PACKAGE__?


When setting up Perl code to run as either a script or module, what is the reason for __PACKAGE__?



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(); 



How do you compare the content of two archive files programmatically?

1:



How can I install a CPAN module into a local directory?
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.


How can I tell if I'm running in a VMWARE virtual machine (from linux)?
If you just say run(@ARGV) you are missing the class information.


How do I include all/some of the “sub modules” in a Perl script?
This only matters if you are doing OO style programming..
How can I handle proxy servers with LWP::Simple?
Put the following in a file named Foo.pm, and then say perl Foo.pm 1 2 3.
How can I use a code ref as a callback in Perl?
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: " . 


Why can't I say print $somehash{$var}{fh} “foo”?
join ", ", map { "[$_]" } @$self; } 1;
Now put the following in Bar.pm, and say perl Bar.pm a b c..
How can I find the free space available on mounted points?
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..



85 out of 100 based on 75 user ratings 1375 reviews