Single & Double Quotes in Perl
You can use double quotes or single quotes around literal strings as follows:#! /usr/bin/perl
print "Hello, world\n";
print 'Hello, world\n';This will produce following result:
Hello, world
Hello, world\n$
There is important different in single and double quotes. Only double quotes
interpolate variables and special characters such as new lines \n where
as single quote does not interpolate any variable or special character. Check
below example where we are using $a as a variable to store a value and later
printing that value:#!/usr/bin/perl
$a = 10;
print "Value of a = $a\n";
print 'Value of a = $a\n';This will produce following result:
Value of a = 10
Value of a = $a\n$
"Here" Documents
You can store or print multi line text with a great comfort. Even you can make use of variables inside "here" document. Below is a simple syntax, check carefully there must be no space between the << and the identifier.An identifier may be either a bare word or some quoted text like we used EOF below. If identifier is quoted, the type of quote you use determines the treatment of the text inside the here document, just as in regular quoting. An unquoted identifier works like double quotes.
#! /usr/bin/perl
$a = 10;
$var = <<"EOF";
This is the syntax for here document and it will continue
Until it encounters an EOF in the first line.
This is case of double quote so variable value will be
Interpolated. For example value of a = $a
EOF
print "$var\n";
$var = <<'EOF';
This is case of single quote so variable value will not be
Interpolated. For example value of a = $a
EOF
print "$var\n";This will produce following result:
This is the syntax for here document and it will continue
until it encounters a EOF in the first line.
This is case of double quote so variable value will be
Interpolated. For example value of a = 10
This is case of single quote so variable value will not be
Interpolated. For example value of a = $a
No comments:
Post a Comment