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 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
  • 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-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 say I have an array like this: Band of Horses - Is There
I am fairly new to Perl programming, but I have a fair amount of
Let say I have a website that allows users to send articles on that
let say I have a post that can be found on page post.php?pid=60 but
Let say I have application menus in a database with their icon images (binary
let say i have one table, which have data like: name status bob single
What is the best workflow taken when connection error occurs. Let say we have
Can anyone share your approach for doing a 'or' query in app-engine? Let say
i have 6 element and i want to select the 5th element (with jQuery)
I come from a computer science. background, but I am now doing genomics. My

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.