Perl last Statement
w3hello.com logo
Perl Basics
Perl Introduction
Perl Installation
Perl Comments
Perl String
Perl Operators
Perl Variables
Perl Arrays
Perl do...while Loop
Perl Environment
Perl Syntax Overview
Perl while Loop
Perl Array with Loops
Perl IF...ELSE statement
Perl IF Statement
Perl if-else
Perl foreach Loop
Perl for Loop
Perl continue Statement
Perl nested Loop
Perl until Loop
Perl next Statement
Perl Array
Perl Loops
Perl File I/O
Perl goto
Perl Socket Programming
Perl Error Handling
Perl Directories
Perl Process Management
Perl stdin
Real Device Vs Emulator Testing: Ultimate Showdown
Perl Formats Writing Reports
Perl Regular Expressions
Perl Create Excel File
A complete guide to CSTE certification
Perl Hashes
Perl Special Variables
Perl do-while Loop
Perl Chop() Vs Chomp()
The Perl DBI
Perl IF...ELSIF statement
Perl print() and say()
Perl Database Access
Perl Formats
Perl last Statement
Perl UNLESS...ELSE statement
Perl Scalars
How to organize Requirements as a Business Analyst
Perl Packages and Modules
Perl goto Statement
Perl Object Oriented
Perl Subroutines
Perl redo Statement
Perl Command Line Arguments | argv
Perl String Escaping Characters
Perl Date and Time
Perl Modules and namespaces
Perl UNLESS...ELSIF statement
Perl Conditional Statements IF...ELSE
Perl Functions and Subroutines
Perl Sending Email
Perl DBI
Perl Versions
Top 100 QTP Interview Questions & Answers
Perl Operator Types
Perl switch Statement
Perl File handling
Perl References
Perl CGI Programming
Object Oriented Programming in PERL
Perl switch
Perl Interview Questions
How PERL is used in Automation Testing
Perl Functions References
Perl Multidimensional Array
Perl unless Statement
Perl Basics
Previous : Perl Formats Next : Perl UNLESS...ELSE statement

Perl last Statement


The last statement in Perl is like break statement in C. It is used inside a loop to exit the loop immediately. In other words, last condition iterates the loop.

The Perl syntax for last statement is given below:

Perl last Statement Example

Following is a simple example showing Perl last statement.

Output:

Please enter an item to know the price:
boots
boots costs is Rs. 1200
Please enter an item to know the price:
top
top is not in our list. We apologise!!

In the above program

  • When user provides a key, hash elements runs via loop and each hash key is compared with the key provided by the user.
  • If match is found, we exit the loop using last statement otherwise search continues.
  • On finding a match result is displayed.

Perl last Statement with LABEL

Using the Perl last statement alone, you can exits only innermost loop. If you want to exit a nested loop, put a label in the outer loop and pass label to the last statement.

If LABEL is specified with last statement, execution drops out of the loop encountering LABEL instead of currently enclosing loop.

The Perl syntax for last statement with LABEL is given below:

Perl last Statement with LABEL Example

Output:

Please enter an item to know the price:
jaggi
boots costs is Rs. 800
Please enter an item to know the price:
jeans
jeans is not in our list. We apologise!!

The above program works in the same way except that it asks the user to enter a search key again if it couldn't find a match.

Two labels OUTER and INNER are used.

Inside foreach loop, if match is found, we'll exit both the loops because OUTER label is passed to the last statement.

Previous : Perl Formats Next : Perl UNLESS...ELSE statement
© Copyright 2018 W3Hello Publishing Limited. All rights reserved.
Contact us | Sitemap