[perl/en] Use more single quotes and explain single vs double quotes (#2710)

* [perl/en] Explain single vs double quotes

Explains an example of variable interpolation and escape codes in a double quoted string.

* add section about interpolating arrays and email in double quotes trap
This commit is contained in:
Dan Book 2017-05-18 06:40:15 -04:00 committed by ven
parent a5833175be
commit 6e3d29f036

View File

@ -37,10 +37,14 @@ use warnings;
# A scalar represents a single value: # A scalar represents a single value:
my $animal = "camel"; my $animal = "camel";
my $answer = 42; my $answer = 42;
my $display = "You have $answer ${animal}s.\n";
# Scalar values can be strings, integers or floating point numbers, and # Scalar values can be strings, integers or floating point numbers, and
# Perl will automatically convert between them as required. # Perl will automatically convert between them as required.
# Strings in single quotes are literal strings. Strings in double quotes
# will interpolate variables and escape codes like "\n" for newline.
## Arrays ## Arrays
# An array represents a list of values: # An array represents a list of values:
my @animals = ("camel", "llama", "owl"); my @animals = ("camel", "llama", "owl");
@ -58,6 +62,18 @@ my $second = $animals[1];
my $num_animals = @animals; my $num_animals = @animals;
print "Number of numbers: ", scalar(@numbers), "\n"; print "Number of numbers: ", scalar(@numbers), "\n";
# Arrays can also be interpolated into double-quoted strings, and the
# elements are separated by a space character by default.
print "We have these numbers: @numbers\n";
# Be careful when using double quotes for strings containing symbols
# such as email addresses, as it will be interpreted as a variable.
my @example = ('secret', 'array');
my $oops_email = "foo@example.com"; # 'foosecret array.com'
my $ok_email = 'foo@example.com';
## Hashes ## Hashes
# A hash represents a set of key/value pairs: # A hash represents a set of key/value pairs: