skrapasor has asked for the wisdom of the Perl Monks concerning the following question:
Basically, change_that is a subroutine/method of Myclass, and I want to pass arguments to it, using standard input. One way would be having change_that use <STDIN> in the actual code of the class, but that would be too unusable. Should I create a separate function for each item of $action? How would you wise Perl Monks do it in the cleanest and most efficient manner? If you need clarification, I would be happy. If you run my code you get a login prompt, then you are allowed to submit any command you want, until you say "logout." But lets say I use command "change that", which will call method "change_that", I want it to prompt the user for the data it needs to change.#!/usr/bin/perl require '/home/me/perl/myclass.pl'; print "Enter your username: "; chomp($username = <STDIN>); print "Enter your password: "; system("stty -echo"); chomp($password = <STDIN>); system("stty echo"); print "\n"; $bot = new Myclass $username, $password; if(!$bot->login){ print "Enter your username: "; chomp($username = <STDIN>); print "Enter your password: "; system("stty -echo"); chomp($password = <STDIN>); system("stty echo"); print "\n"; } my $action = { 'do' => 'do_this', 'change' => 'change_that' }; print "Menu: "; my $menu_item; while(chomp($menu_item = <STDIN>) && $menu_item ne 'logout'){ if (defined $action->{$menu_item}){ my $method = $action->{$menu_item}; $bot->$method; }else{ print "I didn't understand the command.\n"; } print "Menu: "; } if($menu_item eq 'logout'){ $bot->logout; }else{ print "wtf?\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: I'm creating a menu and using the input as methods. How do I use arguments?
by NetWallah (Canon) on Jun 26, 2008 at 00:04 UTC | |
by skrapasor (Novice) on Jun 26, 2008 at 01:12 UTC | |
by NetWallah (Canon) on Jun 26, 2008 at 16:45 UTC | |
by skrapasor (Novice) on Jun 26, 2008 at 16:59 UTC |