Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Repeating an input prompt

by Tanktalus (Canon)
on Nov 28, 2008 at 22:05 UTC ( [id://726681]=note: print w/replies, xml ) Need Help??


in reply to Repeating an input prompt

Two points to critique. First, as is pointed out, $flag isn't a global variable once you discount the fact that you're using global code. Second, don't re-invent wheels. Download and install IO::Prompt, and then you can do something like this:

#!/usr/bin/perl use strict; use warnings; use IO::Prompt; sub main { my @positions = qw(sitting standing reclining); my @locations = ('at the museum', 'in the woods', 'on the beach', 'in the kitc +hen'); my @drinks = qw(scotch beer coke yoohoo); my $flag = 1; do { my $selection = prompt -p => 'The bartender asks what you want:', -onechar => 1, -menu => \@drinks; $flag = do_it( $selection, \@positions, \@locations ); } until $flag == 0; } sub do_it { my $drink = shift; my $positions_ref = shift; my $locations_ref = shift; my $rand_position = $positions_ref->[ rand( @$positions_ref ) ]; my $rand_location = $locations_ref->[ rand( @$locations_ref ) ]; print "You just drank a $drink $rand_location in the $rand_position +position.\n"; if ( $rand_location eq 'on the beach' ) { print "YEEEEEHAAAWWWWWWWWWW!\n"; return 0; } else { return 1; } } main;
Note how I took your global code, pushed it into a sub, and then called it. Now your $flag isn't global. And now you have to pass the positions and locations refs into do_it, whereas before, it wasn't strictly required (due to the global nature of the variables).

I've already taken into account Anomolous Monk's observance on the usage of rand.

Replies are listed 'Best First'.
Re^2: Repeating an input prompt
by Corion (Patriarch) on Nov 29, 2008 at 08:48 UTC

    Please no! IO::Prompt is a module that TheDamian only wrote for his PBP book and it just doesn't work on Win32 and never can, because it does stupid tricks with devices that don't exist on Win32. It has no test suite, it has lots of serious bugs, it has the usual support by TheDamian, that is, none.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://726681]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-25 13:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found