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"; }
In reply to I'm creating a menu and using the input as methods. How do I use arguments? by skrapasor
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |