 |
| |
Perl qw Function
NEW!!!
Check my new resource: Perl How To Tutorial eBooks
The Perl qw function (quote word) uses embedded whitespace separator to split an expression passed to it into a list of elements and returns that list. It is a handy shortcut that you can use to eliminate the quotes in a list area and make your script more readable. In other words, qw will automatically quote string values for you. qw is either known as a function or an operator and it means the same thing. Its syntax form is as follows: In the EXPR it is not allowed to do any interpolation. You can use any set of delimiters, not just the parentheses (for example qw/EXPR/ is another popular way to use it). In the EXPR, the words can be separated with any whitespace, including newlines. You can use the Perl qw operator if each word of the list has no whitespace characters included.
Check my new How To Tutorial eBook (PDF format):to see a lot of fully commented examples that help you use the qw function in your scripts.
In the following example we use single quotes, double quotes and the Perl qw operator to create an array. All the three forms are equivalent:# initialize an array
my @array = ();
@array = ('sweet', 'bitter', 'sour', 'salty', 'hot'); # or
@array = ("sweet", "bitter", "sour", "salty", "hot"); # or
@array = qw/sweet bitter sour salty hot/;
As you can see in this example, if you have a longer list of words and you don’t use the Perl qw function, you need to type yourself a lot of commas and quotes to separate the words and your script can become difficult to read. But don’t mix the commas with the whitespace to separate the words because this will produce warnings if you will run the script with the warnings enabled.You could also populate a hash using the Perl qw function, by alternating the strings in the expression argument, like in the following example: # initialize a hash
my %hash = ();
# populate the hash with a few elements
%hash = qw(1 one 2 two 3 three 4 four);
# print the %hash
while (my ($key, $val) = each %hash) {
print "$key=>$val\n"
}
This code will output:4=>four
1=>one
3=>three
2=>two
You can use the qw function when you have a list of predefined strings, for example the text messages associated with some error codes:# initialize a hash
my %errorCodes = ();
# populate the hash with a few elements
%errorCodes = qw(
0 SUCCESS
1 FILE_NOT_FOUND
2 OUT_OF_MEMORY
);
# sort and print the %errorCodes hash
foreach my $key (sort keys %errorCodes) {
print "$key=>$errorCodes{$key}\n";
}
Here’s the output:0=>SUCCESS
1=>FILE_NOT_FOUND
2=>OUT_OF_MEMORY
Please click here to download the Perl qw script with all the above examples included.
Check my new How To Tutorial eBook (PDF format):to see a lot of fully commented examples that help you use the qw function in your scripts.
NEW!!!
Do you want more information about the basic Perl topics?
Check my new "Perl How To" Tutorial eBooks page where I'll answer the most frequent questions regarding some topics :
Perl How To Tutorial eBooks
Table of Contents:
A Perl Script Install Perl Running Perl Perl Data Types Perl Variables Perl Operators Perl Lists Perl Arrays Array Size Array Length Perl Hashes Perl Statements Perl if Perl unless Perl switch Perl while Perl do-while Perl until Perl do-until Perl for Perl foreach Built-in Perl Functions Functions by Category String Functions List Functions Array Functions Hash Functions Miscellaneous Functions Functions in alphabetical order chomp chop chr crypt defined delete each grep hex index join lc lcfirst length map oct ord pack pop push q qq qw reverse rindex scalar shift sort splice split sprintf substr tr uc ucfirst undef unshift
return from Perl qw function to Perl Basics
Would you like to create your own website like this one? Hit the Alarm Clock!

|
|