skrapasor has asked for the wisdom of the Perl Monks concerning the following question:

I don't think it's a good idea to include standard input for class methods, because then those functions couldn't be called without them waiting for input which there probably is none of. Anyway, here is my script:
#!/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"; }
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.
  • Comment on I'm creating a menu and using the input as methods. How do I use arguments?
  • Download Code

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
    To accomplish what you asked for, I would use callbacks.

    You can call $method like this:

    $bot->$method(\&GetInput, \&ShowOutput); # In your main program, you have to define sub GetInput{...}, # and sub ShowOutput{...} # Alternatively, use anonymous subs : $bot->$method(sub{print $_[0]; return scalar <>}, sub{print $_[0] . "\n"});
    (Untested).

    To complete the picture, here is how $method would use the passed callbacks ..

    sub method{ my ($self, $getInput,$SendOutput) = @_; my $input = $getInput->("Optional PROMPT parameter, if any\n"); #.. Do something with $input, and generate $output $SendOutput->($output); }

         Have you been high today? I see the nuns are gay! My brother yelled to me...I love you inside Ed - Benny Lava, by Buffalax

      I'm not sure what the GetInput subroutine would be. It seems like it would be the same for every method, but some methods need multiple parameters and the prompt would be different, how would I implement that?
        You have the option to use a single "GetInput" subroutine, or pass references to different subs to different methods.

        Your implementation of "GetInput" can use as many parameters as you like - you can make some, or all optional - that way you could use a single subroutine for all methods.

        In the example I showed, you certainly CAN pass different prompts back to GetInput.

        I had already given you sample code for GetInput - you can use the contents of the first Anonymous sub.

        I would suggest you try implementing the suggestion I provided, and post back specific problems you run into - we monks are happy to help you understand and troubleshoot specific issues, but are reluctant to solve the entire problem for you. This helps you become self-sufficient, while reducing our liability for misunderstanding your issues.

             Have you been high today? I see the nuns are gay! My brother yelled to me...I love you inside Ed - Benny Lava, by Buffalax