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
  • 0 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

Ask A Question

Stats

  • Questions 161k
  • Answers 161k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I haven't tried this approach, but you can take a… May 12, 2026 at 11:46 am
  • Editorial Team
    Editorial Team added an answer Initial question: window.location.href.substr(0, window.location.href.indexOf('#')) or window.location.href.split('#')[0] both will return the… May 12, 2026 at 11:46 am
  • Editorial Team
    Editorial Team added an answer Timing my application with an input of 10Mb in size… May 12, 2026 at 11:46 am

Related Questions

The C++ friend keyword allows a class A to designate class B as its
Fluent interfaces is a fairly hot topic these days. C# 3.0 has some nice
Ok...so ive been wriggling around with this piece of cod for quite a while
I was going to Ask a Question earlier today when I was presented to
You may think this question is like this question asked on StackOverflow earlier. But

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.