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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:03:04+00:00 2026-06-03T11:03:04+00:00

I wrote the following Perl Class: package Menu; use strict; my @MENU_ITEMS; my $HEADER

  • 0

I wrote the following Perl Class:

package Menu;
use strict;

my @MENU_ITEMS;
my $HEADER = "Pick one of the options below\n";
my $INPUT_REQUEST = "Type your selection: ";

sub new {
    my $self = {};
   $self->{ITEM}   = undef;
   $self->{HEADER} = undef;
   $self->{INPUT_REQUEST} = undef;
   bless($self);
   return $self;
}

sub setHeader {
    my $self = shift;
    if(@_) { $self->{HEADER} = shift }
    $HEADER = $self->{HEADER}."\n";
}

sub setInputRequest {
    my $self = shift;
    if(@_) { $self->{INPUT_REQUEST} = shift }
    $INPUT_REQUEST = $self->{INPUT_REQUEST}." ";
}

sub addItem {
    my $self = shift;
    if(@_) { $self->{ITEM} = shift }
    push(@MENU_ITEMS, $self->{ITEM}); 
    }

sub getMenu {
    my $formatted_menu .= $HEADER;
    my $it=1;
    foreach(@MENU_ITEMS) {
        $formatted_menu.=$it.". ".$_."\n";
        $it++
      }
    $formatted_menu.=$INPUT_REQUEST;
    return $formatted_menu;
}
1;

If I call the following perl script:

#!/usr/bin/perl
use strict;
use Menu;

my $menu = Menu->new();
$menu->addItem("First Option");
$menu->addItem("Second Option");
print $menu->getMenu;

I’ll get the following output:

Pick one of the options below
1. First Option
2. Second Option
Type your selection:

I’d like to modify given class in a way that I can pass a second argument to the method addItem()

something like:
$menu->addItem("First Option", &firstOptionFunction());

and if and only if First Option is selected, then $firstOptionFunction is executed.

Is there any way to achieve such behavior in Perl?
Thanks!

  • 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-03T11:03:07+00:00Added an answer on June 3, 2026 at 11:03 am

    You would want to pass a reference to the subroutine.

    $menu->addItem("First Option", \&firstOptionFunction);
    

    And your addItem method might look like this:

    sub addItem { ## your logic may vary
        my ( $self, $option, $code ) = @_;
        if ( $option eq 'First Option' ) {
            $code->();
        }
    
        $self->{ITEM} = $option;
        push @MENU_ITEMS, $option;
    
        return;
    }
    

    As you mentioned in the comments, you might want to not pass the subroutine as a reference, but rather store it somewhere else. Something like this might work:

    sub new {
        my $class = shift;
        my $self = bless {}, $class;
        $self->{f_o_code} = \&firstOptionFunction; ## use a better name than f_o_code
        return $self;
    }
    
    ## add your other methods
    
    sub addItem { ## your logic may vary
        my ( $self, $option ) = @_;
        if ( $option eq 'First Option' ) {
            $self->{f_o_code}->();
        }
    
        $self->{ITEM} = $option;
        push @MENU_ITEMS, $option;
    
        return;
    } ## call like $menu->addItem( 'First Option' );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote the following Perl script (below) in order to create simple XML file.
I have a question with the following Code: #!/usr/bin/perl use strict; use warnings; my
I have an interesting problem. I wrote the following perl script to recursively loop
I wrote the following regex in perl and now i want to write it
I wrote the following program #include <iostream> template<typename C, typename Res, typename... Args> class
please look at the following code first. #! /usr/bin/perl package foo; sub new {
I wrote the following Perl function sub Outputing { my $featureMatrix = shift; my
I wrote following code: class node: def __init__(self, title, series, parent): self.series = series
In order to list pathes in Windows,I wrote below Perl function(executed under StrawBerry runtime
I wrote a very simple Perl script that contains the following line. my $q_it

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.