10gbPearlTeacher


Perl

10 Green Bottles

by Rob Poulter

 

 

# 10 Green Bottles in Perl
my $bottles = 10; # original number of bottles

# for loop to iterate through the bottles in reverse order
for (my $i = $bottles; $i > 0; $i--)
{
  # Default plural for this number of bottles
  my $plural = "s";
  # Change the plural to none if there is only one bottle
  $plural = "" if ($i == 1);
  # The plural of the next number of bottles
  my $next_plural = "s";
  # Change the plural if the current number of bottles is two
  $next_plural = "" if ($i == 2);
  # How many bottles are there next time?
  my $next = $i - 1;
  # Change next to the word NO if there is only one bottle now
  $next = "NO" if ($i == 1);
  print "$i green bottle$plural, sitting on the wall!n";
  print "And if one green bottle should accidentally fall,n";
  print "There'd be $next green bottle$plural, sitting on the wall!nn";