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

  • SEARCH
  • Home
  • 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 665707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:44:18+00:00 2026-05-13T23:44:18+00:00

Let us say if I have a Perl module Resounces.pm with this code snippet

  • 0

Let us say if I have a Perl module Resounces.pm with this code snippet

my $my_class = new MY_CLASS;
our resource => { '/list' => $my_class->list_files() };

So can I call this resource variable in another perl script something similar like this?

use Resources;
$Resources::resource->{'/list'};

My intention is to execute list_files() method when I use that last statement. Is that even possible?

  • 1 1 Answer
  • 1 View
  • 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-13T23:44:18+00:00Added an answer on May 13, 2026 at 11:44 pm

    One option is to use an anonymous sub:

    package Resources;
    my $my_class = new MY_CLASS;
    our $resource = { '/list' => sub { $my_class->list_files(@_); } };
    
    ...
    
    package main;
    # dereference the code ref
    &{$Resources::resource->{'/list'}};
    # or call it:
    $Resources::resource->{'/list'}();
    

    Should you change what $my_class references, it will also be changed in the anonymous function. This may be what you want; if not, keep reading for a function (bindMethod) that binds an object and a method.

    If you don’t want the anonymous function to appear on the stack:

    our $resource = { '/list' => sub { unshift @_, $my_class; 
                                       goto &MyClass::list_files; } };
    

    Note that this form of goto isn’t the usual goto statement—it’s a tail call.

    You could define a helper method to set up the binding, though honestly it doesn’t gain you much.

    sub bindMethod {
        my ($obj, $meth) = @_;
        return sub { unshift @_, $obj; goto &{$meth}; }
    }
    
    my $my_class = new MyClass;
    our $resource = { '/list' => bindMethod($my_class, \&MyClass::list_files) };
    

    Here’s a version of bindMethod that doesn’t require the class to be specified with the method name.

    sub bindMethod {
        my ($obj, $meth) = @_;
        if (! ref $meth) {
            my $class = ref $obj;
            $meth = \&{"${class}::$meth"};
        }
        return sub { unshift @_, $obj; goto &{$meth}; }
    }
    ...
    my $my_class = new MyClass;
    our $resource = { '/list' => bindMethod($my_class, 'list_files') };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a perl module file and I want to include and
Let's say in Perl I have a list of hash references, and each is
Let's say I have this #!/usr/bin/perl %x = ('a' => 1, 'b' => 2,
Let say I have the following code $(p).bind(click, function(){ alert( $(this).text() ); }); When
Let's say I have a Perl class that has a DESTROY method. This method
Let say I have abstract class called: Tenant and Customer. The tenant in this
Let's say I have a list of elements @list=(1,2,3); #desired output 1,2,3 and I
For the sake of simplicity, let's say that we have input strings with this
Let's say I have a Perl file in which there are parts I need
Let say I have this table, A B ------- 1 A 2 C 3

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.