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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:16:41+00:00 2026-05-26T07:16:41+00:00

in Perl I know you can use eval and *{$func_name} to call functions dynamically

  • 0

in Perl I know you can use eval and *{$func_name} to call functions dynamically but how do you do this with methods of an object?

for example

EZBakeOven
  sub make_Cake { ... }
  sub make_Donut { ... }
  sub make_CupCake { ... }
  sub make_Soup { ... }

  sub make{
    my($self,$item) = @_;
    if( defined $self->make_$item ){ #call this func if it exists
      $self->make_$item( temp => 300, with_eggs => true ); 
    }
  }

so that if I say something like

$self->make('Cake');
#or maybe I have to use the full method name
$self->make('make_Cake');

it will call

$self->make_Cake();
  • 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-26T07:16:42+00:00Added an answer on May 26, 2026 at 7:16 am

    You should be able to do something like:

    sub make {
      my ($self, $item) = @_;
      my $method = "make_$item";
      $self->$method(whatever);
    }
    

    Edit: You might want to also use can() to make sure you’re calling a method that can be called:

    sub make {
      my ($self, $item) = @_;
      my $method = "make_$item";
      if ($self->can($method)) {
        $self->$method(whatever);
      } else {
        die "No such method $method";
      }
    }
    

    Edit 2: Actually, now that I think about it, I’m not sure if you really can do that. Code I’ve written before does something like that, but it doesn’t use an object, it uses a class (so you’re calling a specific function in a class). It might work as well for objects, but I can’t guarantee it.

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

Sidebar

Related Questions

I know how to use Perl's Getopt::Long, but I'm not sure how I can
I know with OO Perl I can have objects and inheritance, but are interfaces
I don't know Perl. I don't even love it, but i'm obliged to use
Does anyone know where online copies of the old The Perl Journal articles can
I know there is a way to call Perl routines from C. As shown
Here is my situation: I know almost nothing about Perl, but it is the
I need to know how what command or commands I can use in a
I know you can use the file test operator -B to test if a
Is there a function i can use in Perl to sanitize input before putting
I really should know this, but I've worked mainly with Linux, Mac OS X

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.