Skip to content
Related Articles

Related Articles

Perl | qw Operator
  • Last Updated : 07 May, 2019

The qw operator in Perl is used to extract each element of the given string as it is in an array of elements in single-quote ( ‘ ‘ ).
This function stands for quote word because it considers each word of the given string as it is quoted like qw(Geeks for Geeks) is equivalent to (‘Geeks’, ‘for’, ‘Geeks’).
This qw() uses parentheses so it seems like it is a function but it is not. It uses different types of delimiters which are shown below:

@String = qw/Ram is a boy/;
@String = qw{Geeks for Geeks};
@String = qw[Geeks for Geeks];
@String = qw'Geeks for Geeks';
@String = qw"Geeks for Geeks";
@String = qw!Geeks for Geeks!;
@String = qw@Geeks for Geeks@;

Syntax: qw(String)

Parameters:
String : It is the input string whose each element is extracted.

Returns: each element of the given string as it is an array of elements in single-quote (”).

Example 1:






#!/usr/bin/perl
  
# Initialising a String as the parameter of qw
# operator whose each element is extracted.
@String = qw(GfG is a computer science portal);
foreach $key (@String) {
   print"Element is: $key\n";
}


Output:

Element is: GfG
Element is: is
Element is: a
Element is: computer
Element is: science
Element is: portal

Example 2:




#!/usr/bin/perl
  
# Initialising a String as the parameter of qw
# operator whose each element is extracted.
@String = qw(Delhi Mumbai Kolkata Patna);
foreach $key (@String) {
   print"City name is: $key\n";
}

Output :

City name is: Delhi
City name is: Mumbai
City name is: Kolkata
City name is: Patna

Note: The qw operator reads a white-space and extract elements before and after the white-space.

My Personal Notes arrow_drop_up
Recommended Articles
Page :