Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 301005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:57:57+00:00 2026-05-12T06:57:57+00:00

In this earlier Stackoverflow question and especially brian d foy’s How a Script Becomes

  • 0

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();
  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:57:58+00:00Added an answer on May 12, 2026 at 6:57 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

You may think this question is like this question asked on StackOverflow earlier. But
Earlier this week I ask a question about filtering out duplicate values in sequence
(This is technically an addendum to an earlier StackOverflow question I had posted, but
I had this question earlier and it was concluded it was a bug in
This is related to my earlier question , but I thought I'd simplify it
Earlier I asked this question How to correctly unit test my DAL? , one
This is closely related to an earlier question . In the managed world: How
This is in reference to a question I asked earlier. Aside from viewing the
This is an attempt to rephrase a question I asked earlier. I'd like to
Earlier I asked why this is considered bad: class Example { public: Example(void); ~Example(void);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.