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 8029405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:25:42+00:00 2026-06-05T00:25:42+00:00

Given a variable containing a string that represents the name of a package, how

  • 0

Given a variable containing a string that represents the name of a package, how do I call a specific subroutine of the package?

Here’s the closest thing I have figured out:

package MyPackage;

sub echo {
    print shift;
}

my $package_name = 'MyPackage';
$package_name->echo('Hello World');

1;

The problem with this code is the subroutine is called as a class method; the package name is passed in as the first argument. I want to invoke the subroutine from the package name without a special first argument being implicitly passed in.

  • 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-06-05T00:25:43+00:00Added an answer on June 5, 2026 at 12:25 am

    Perl method calls are just regular subroutines, which get the invocant as the first value.

    use strict;
    use warnings;
    use 5.10.1;
    
    {
      package MyPackage;
      sub new{ bless {}, shift } # overly simplistic constructor (DO NOT REUSE)
      sub echo{ say @_ }
    }
    
    my $package_name = 'MyPackage';
    $package_name->echo;
    
    my $object = $package_name->new();
    $object->echo; # effectively the same as MyPackage::echo($object)
    
    MyPackage
    MyPackage=HASH(0x1e2a070)
    

    If you want to call a subroutine without an invocant, you will need to call it differently.

    {
      no strict 'refs';
      ${$package_name.'::'}{echo}->('Hello World');
      &{$package_name.'::echo'}('Hello World');
    }
    
    # only works for packages without :: in the name
    $::{$package_name.'::'}{echo}->('Hello World');
    
    $package_name->can('echo')->('Hello World');
    
    • The can method returns a reference to the subroutine that would be called if it had been called on the invocant. The coderef can then be used separately.

      my $code_ref = $package_name->can('echo');
      $code_ref->('Hello World');
      

      There are some caveats to using can:

      • can may be overridden by the package, or any class from which it inherits.
      • The package that defines a method may be different than the invocant.

      This may actually be the behaviour you’re looking for though.

    • Another approach is to use something called a symbolic reference.

      {
        no strict 'refs';
        &{ $package_name.'::echo' }('Hello World');
      }
      

      Using symbolic references is usually not recommended. Part of the problem is that it is possible to accidently use a symbolic reference where you didn’t intend on using one. This is why you can’t have use strict 'refs'; in effect.

      This may be the simplest way to do what you want to do though.

    • If you don’t want to use a symbolic reference you could use the Stash.

      $MyPackage::{echo}->('Hello World');
      $::{'MyPackage::'}{echo}->('Hello World');
      
      $main::{'MyPackage::'}{echo}->('Hello World');
      $main::{'main::'}{'MyPackage::'}{echo}->('Hello World');
      $main::{'main::'}{'main::'}{'main::'}{'MyPackage::'}{echo}->('Hello World');
      

      The only problem with this is that you would have to split $package_name on ::

      *Some::Long::Package::Name::echo = \&MyPackage::echo;
      
      $::{'Some::'}{'Long::'}{'Package::'}{'Name::'}{echo}('Hello World');
      
      sub get_package_stash{
        my $package = shift.'::';
        my @package = split /(?<=::)/, $package;
        my $stash = \%:: ;
        $stash = $stash->{$_} for @package;
        return $stash;
      }
      get_package_stash('Some::Long::Package::Name')->{echo}('Hello World');
      

      This isn’t that big of a problem though. After a quick look on CPAN you find Package::Stash.

      use Package::Stash;
      my $stash = Package::Stash->new($package_name);
      my $coderef = $stash->get_symbol('&echo');
      $coderef->('Hello World');
      

      (The Pure Perl version of Package::Stash uses symbolic references, not the Stash)


    It’s even possible to make an alias of the subroutine/method, as if had been imported from a module that was using Exporter:

    *echo = \&{$package_name.'::echo'};
    echo('Hello World');
    

    I would recommend limiting the scope of the alias though:

    {
      local *echo = \&{$package_name.'::echo'};
      echo('Hello World');
    }
    

    This is an exception, where you can use a symbolic reference with strict 'refs' enabled.

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

Sidebar

Related Questions

I have the following code. Given that the variable u1 can be any of
Given that I have a variable $peopleSize in the format (I've already extracted the
The purpose of ThreadLocal as given here states that the variable is local to
I have a variable (x) containing a character string (b). This string gives the
Does anyone have a recommended pseudo-algorithm for, given a string containing an address: Break
Given a variable isolated from a data frame, how does one obtain it's name?
Given a variable which holds a string is there a quick way to cast
Given <xsl:variable name=datePrecision as=element()*> <p>Year</p> <p>Month</p> <p>Day</p> <p>Time</p> <p>Timestamp</p> </xsl:variable> The expression $datePrecision[5] returns
I have this given array of ids assigned to a certain variable, $post_id =
I have a variable in Python containing a floating point number (e.g. num =

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.