BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Perl – foreach loop with example

Last Updated: September 10, 2022 by Chaitanya Singh | Filed Under: Perl

The foreach loop is used for iterating arrays/lists.

Syntax of foreach loop:

foreach var (list) {
   statement(s);
}

Flow of Execution of the foreach Loop

In foreach loop, the loop continues execution until all the elements of the specified array gets processed.

Example

#!/usr/local/bin/perl

@friends = ("Ajeet", "Tom", "Steve", "Lisa", "Kev");

foreach $str (@friends){
   print "$str\n";
}

Output:

Ajeet
Tom
Steve
Lisa
Kev

Top Related Articles:

  1. Use strict and use warnings in Perl
  2. Perl – Lists and Arrays
  3. Data Types in Perl
  4. Installing Perl on Windows, Mac, Linux and Unix
  5. Perl Variables

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Perl Tutorial

  • Perl Tutorial
  • Perl Installation
  • First Perl Program
  • Perl Syntax
  • Data types in Perl
  • Perl Variables
  • my keyword
  • Perl Scalars
  • Use strict and use warnings
  • Perl Arrays
  • Perl Hashes
  • Operators in Perl
  • Perl Conditional statements
  • Perl if
  • Perl if-else
  • Perl if-elsif-else
  • Perl unless
  • Perl unless-else
  • Perl unless-elsif-else
  • Perl switch case
  • Perl given-when-default
  • Perl loops
  • Perl subroutines
  • Perl Strings
  • Perl Escape Sequences

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap