We help IT Professionals succeed at work.
Get Started

how to pass arguments to a sub routine using hashes

Last Modified: 2010-03-05
I am trying to make CLI. when the script starts the user will type in some command and script performs the required action.

i have used hashes for purpose of efficiency. what i do is that the command is the key of the hash and the value of the hash is a reference to a sub routine.

now i cant figure out how to pass arguments to such functions.

here is my code

sub settime($){ print "nMetrics>You have entered the settime command\n";
                        print "nMetrics>The argument given is $_[0]\n";}
        sub showtime{ print "nMetrics>You have entered the show time command\n";}
        sub error{ print "nMetrics>Invalid command line options\n";}

        $myhash{"set time"}=\&settime;
        $myhash{"show time"}=\&showtime;

        print "Welcome to nMetrics application monitor\n";
        print "Please type in a command\n";

        label: print "nMetrics>";
        while(<STDIN> )
                if (exists $myhash{$_})

        goto label;

now with this code i can only type in a command. if the user also has to pass some arguments with the command i cant do that.

how will i pass arguments to my sub routines?
Watch Question
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE