'last' can stop loop immediately, when you see 'last', the loop is over.
 
while(){
 if(/__END__/){
 last;
 }elsif(/fred/){
 print;
 }
}
## last jumps to here
 
Whenever input contains line '__END__', the loop ends adn Perl executes the rest of codes.
 
'next' immediately stops the current time of loop, and go to the bottom of loop and continue the next time of loop.
 
while(<>){
 foreach(split){ # split $_ into words, give each word to $_
 $total++;
 next if /\W/; # if not word, jumps from the rest of expressions
 $valid++;
 $count{$_}++; # caculate appears of each words
 ## above 'next' jumps to here
 }
}
print "total things = $total, valid words = $valid\n";
foreach $word(sort keys %count){
 print "$word was seen $count{$word} times.\n";
}
 
'redo' redo the current time of time of loop, does not test any condition or go to the next time of loop.
 
my @words=qw{ Fred barney pebbles dino wilma betty };
my $errors=0;
foreach(@words){
 print "Type the word '$_': ";
 chomp(my $try=);
 if($try ne $_){
  print "Sorry - That's not right.\n\n";
  $errors++;
  redo;
 }
}
print "You've completed the test, with $errors errors.\n";
 
foreach(1..10){
 print "Iteration number $_.\n\n";
 print "Please choose: last, next, redo, or none of the above? ";
 chomp(my $choice=);
 print "\n";
 last if $choice =~ /last/i;
 next if $choice =~ /next/i;
 redo if $choice =~ /redo/i;
 print "That wasn't any of the choices... onward!\n\n";
}
print "That's all, folks!\n";
 
Lable can be used for last/next/redo, they'll go to the lable position.
 
LINE: while(<>){
 foreach(split){
  last LINE if /__END__/; # jump out of loop at LINE
  ...
 }
}
 
condition ? true expression : false expression
 
my $location=&is_weekend($day) ? "home" : "work";
my $average=$n?($total/$n):"-----";
print "Average: $average\n";
 
my $size=
 ($width<10) ? "small" :
 ($width<20) ? "medium" :
 ($width<50) ? "large" :
     "extra-large"; # default
 
&& means "and", || means "or" in condition
 
if((9<=$hour)&&($hour<17)){
 print "Aren't you supposed to be at work...?\n";
}
 
if(($name eq 'fred')||($name eq 'barney')){
 print "You're my kind of guy!\n";
}
 
Exercise: Write a program, ask user to input a number between 1 and 100 to match the given randon digit. If high or low tell him about it and retry, if matches or user inputs quit/exit/enter then the program ends.

#!/usr/bin/perl

$num=int(1 + rand 100);
print "Please guese the number between 1 and 100: ";
while(<>){
    chomp if $_ ne "\n";
    exit if $_==$num || /quit|exit|\n/;
    if($_>$num){
        print "Too high, please retype: ";
    }elsif($_<$num){
        print "Too low, please retype: ";
    }
    next;
}