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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:37:03+00:00 2026-05-25T09:37:03+00:00

I want to call a main controller function that dispatches other function dynamically, something

  • 0

I want to call a main controller function that dispatches other function dynamically, something like this:

package Controller;

my %callback_funcs = ();

sub register_callback{
   my ($class,$callback,$options) = _@;
   #apppend to %callback_funcs hash ... ?
}

sub main{
%callback_funcs = ( add => 'add_func', rem => 'remove_func', edit => 'edit_func');  
  while(<STDIN>){
     last if ($_ =~ /^\s*$/);
     if($_ == 'add' || _$ == 'rem' || _$ == 'edit'){
        $result = ${callback_funcs['add']['func']}(callback_funcs['add']['options']);
     }
  }
}

sub add_func{
...
}

One caveat is that the subs are defined in other Modules, so the callbacks would have to be able to reference them… plus
I’m having a hard time getting the hashes right!

  • 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-25T09:37:03+00:00Added an answer on May 25, 2026 at 9:37 am

    So, it’s possible to have a hash that contains anonymous subroutines that you can invoke from stdin.

    my %callbacks = (
        add => sub {
            # do stuff
        },
        fuzzerbligh => sub {
            # other stuff
        },
    );
    

    And you can insert more hashvalues into the hash:

    $callbacks{next} = sub {
        ...
    };
    

    And you would invoke one like this

    $callbacks{next}->(@args);
    

    Or

    my $coderef = $callbacks{next};
    $coderef->(@args);
    

    You can get the hashkey from STDIN, or anywhere else.

    You can also define them nonymously and then take a reference to them.

    sub delete {
        # regular sub definition
    }
    
    $callbacks{delete} = \&delete;
    

    I wouldn’t call these callbacks, however. Callbacks are subs that get called after another subroutine has returned.

    Your code is also rife with syntax errors which may be obscuring the deeper issues here. It’s also not clear to me what you’re trying to do with the second level of arrays. When are you defining these subs, and who is using them when, and for what?

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

Sidebar

Related Questions

I want to call a function from a .NET DLL (coded in C#) from
I want to call a c# function from my javascript function. I have a
I want to call a function (which calculates my version number) when my NSIS
i want to call 2 views within one controller in spring with java. for
actually i just want to show main address of my site like http://localhost/website/ and
I want to call a member function which is virtual (inheritance is used in
Actually i want to call a function of one activity from another activity..i write
This post is related to: POSIX TIMER- Have multiple timers I want to call
I have a medium-complex Rails app. The main controller (the one that does what
I have a Python script that I want to use as a controller to

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.