From 6e3d29f036f9ede844f17e36e764b685d046adf9 Mon Sep 17 00:00:00 2001 From: Dan Book Date: Thu, 18 May 2017 06:40:15 -0400 Subject: [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 --- perl.html.markdown | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'perl.html.markdown') diff --git a/perl.html.markdown b/perl.html.markdown index 93eabea9..17a538e3 100644 --- a/perl.html.markdown +++ b/perl.html.markdown @@ -37,10 +37,14 @@ use warnings; # A scalar represents a single value: my $animal = "camel"; my $answer = 42; +my $display = "You have $answer ${animal}s.\n"; # Scalar values can be strings, integers or floating point numbers, and # 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 # An array represents a list of values: my @animals = ("camel", "llama", "owl"); @@ -58,6 +62,18 @@ my $second = $animals[1]; my $num_animals = @animals; 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 # A hash represents a set of key/value pairs: -- cgit v1.2.3