Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl - Difference between next and redo

Tags:

perl

When I am executing a program both giving the same result. Please explain the difference between 'next' & 'redo'.

like image 791
Ravi Shanker Reddy Avatar asked Nov 28 '22 09:11

Ravi Shanker Reddy


1 Answers

redo, next and last are used inside loop blocks in Perl. Most often you will see them in for or while blocks, but you can use them in a bare block if you need to

The essential difference is

  • redo jumps to the beginning of the block -- the opening brace {
  • next jumps to the end of the block -- the closing brace }
  • last jumps out of the block altogether

next and last are only different for the blocks of while and for loops (and for blocks that have a continue section)

So you could write a loop like this

my $n;
{
    ++$n;
    print $n, "\n";
    redo if $n < 10;
}

which would print the numbers from 1 to 10

like image 61
Borodin Avatar answered Dec 25 '22 04:12

Borodin