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.


In reply to Re: Repeating an input prompt by Tanktalus
in thread Repeating an input prompt by zod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.