in reply to Advice on goto usage?

No-one else seems to have pointed out the idiom for this:
INPUT: { print("Type R to re-enter the values or Type E to exit:\n"); chomp( my $value = <STDIN> ); if ( $value eq 'E' ) { exit; } if ( $value ne 'R' ) { print("Invalid entry, try again:\n"); } redo INPUT; }

This is a bare-block with redo. You can also use next and last (which are synonymous in a bare-block) to leave the block and continue processing at the first line after the block. This is not considered 'goto' because redo/next/last are forced to work within either the innermost loop/bare block or with labelled loop/bare blocks. You can't redo to a labelled line, like you can with goto.


  • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
  • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"

Replies are listed 'Best First'.
Re^2: Advice on goto usage?
by tlm (Prior) on May 05, 2005 at 14:01 UTC

    You can also use next and last (which are synonymous in a bare-block)...

    ...except for the way they handle continue blocks:

    use strict; use warnings; { next; } continue { print "this prints...\n"; } { last; } continue { print "...but this doesn't\n"; } __END__ this prints...

    the lowliest monk