Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-29-2005, 11:36 PM   #1
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Rep: Reputation: 15
Problem with passing arguments in Perl

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>";
                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?
Old 05-30-2005, 01:47 AM   #2
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 30
Mayhap something like this?

while (1) {

  chomp($input = <STDIN>);

  # this splits the input on whitespace
  @args = split /\s+/, $input;

  # the first argument should have been the command
  $cmd = shift @args;

  # do whatever with $cmd and @args...
  $coderef = $myhash{$cmd}; 

  if ($coderef and @args) {
    # valid command with args
  } elsif ($coderef) {
    # valid command, no args
  } else {
    # invalid command

I'm thinking you could also just go ahead and pass the @args array even if it's empty; an empty list should look the same to the called function as no arguments at all.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing arguments to a shell script subu_s Programming 3 09-02-2005 05:13 AM
passing a list of arguments to a command hdagelic Linux - General 2 05-09-2005 09:30 AM
Passing Arguments into the Thread Function George_gk Programming 2 01-31-2005 05:03 AM
Handline passing arguments in C AMMullan Programming 9 03-22-2004 01:37 AM
Issue with recursive script passing arguments gauge73 Linux - Newbie 0 01-06-2004 07:16 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:16 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration