in reply to What's the point of a labeled block without a loop?

I use it sometimes for processing STDIN (the few times I ever need to). redo is your friend.
my $age; INPUT: { print "Please enter your age: "; $age = <STDIN>; if ( $age !~ /\D/ ) { last INPUT; } redo INPUT; }

My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Replies are listed 'Best First'.
Re^2: What's the point of a labeled block without a loop?
by BrowserUk (Patriarch) on Dec 17, 2005 at 00:10 UTC

    Is there any specific advantage to the label there?

    That appears to work just as well with none (except you need a chomp).

    I'm not much taken with "if $age does not contain a non-digit, were done". How about:

    my $age; { print "Please enter your age: "; chomp( $age = <STDIN> ); redo if $age =~ /\D/; } Please enter your age: Mind your own Please enter your age: As old as m'tongue and a little older than m'teeth. Please enter your age: 300000000000

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      The only real benefit is reader clarity. By labelling it, you're giving a reason for a bare-block.

      Also, my personal style - I (mostly) always put a label with every last and redo. (I don't for next because next is much more common.)


      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
        By labelling it, you're giving a reason for a bare-block.

        In this specific case, it can be done without a bare block:

        my $age; do { print "Please enter your age: "; chomp ($age = <STDIN>); } while $age =~ /\D/; print "Got it: $age\n"; __END__ Please enter your age: hi there Please enter your age: 123abc Please enter your age: 99 Got it: 99

        --
        David Serrano

        By labelling it, you're giving a reason for a bare-block.
        Huh? Do you really mean to say that the label is the reason for the bare block?
        Perl --((8:>*