1 min

Quotes in Perl

http://perlmaven.com/quoted-interpolated-and-escaped-strings-in-perl

http://www.perlmonks.org/?displaytype=print;node_id=401006

Single quotation marks are used to enclose data you want taken literally.

Double quotation marks are used to enclose data that needs to be interpolated before processing.

q

The first way to quote without quotes is to use q() notation. Instead of using quotation marks, you would use parentheses with a q preceding them:

$bar = q(it is 'worth' $foo);

This example, when run, produces the following:
it is 'worth' $foo

qq

In the same way that double-quotes add interpolation to the functionality of single-quotes, doubling the q adds interpolation to quoting without quotation marks. For instance, if you wanted to avoid escape characters and interpolate $foo in the above code, and wanted to use double-quotes around the word worth, you might do this:

#!/usr/bin/perl -w
use strict;
my $foo;
my $bar;
$foo = 7;
$bar = qq(it is "worth" $foo);
print $bar;

This example, when run, produces the following:
it is "worth" 7

qw

You can use qw to quote individual words without interpolation. Use whitespace to separate terms you would otherwise have to separate by quoting individually and adding commas. This is often quite useful when assigning lists to array variables. The two following statements are equivalent:

@baz = ('one', 'two', 'three');
@baz = qw(one two three);

#perl
#programming

From JR's : articles
213 words - 1450 chars - 1 min read
created on - #
source - versions

Related articles
Perl regex extracting domain name from URL - Oct 02, 2013
Perl programming tools to test - Nov 14, 2014
Perl Programming Environments - May 06, 2013
Looping through Perl data types - Nov 25, 2013
Scaup web publishing app update as of Feb 21, 2015 - Mar 02, 2015
more >>



A     A     A     A     A

© 2013-2017 JotHut - Online notebook

current date: Dec 12, 2024 - 11:39 p.m. EST