Literals are constant values that you embed directly in the program code. Perl supports both string literals and numeric literals.
String literals are enclosed by single quotes (') or double quotes ("):
'The quality of mercy is not strained.'; # a single-quoted string "The quality of mercy is not strained."; # a double-quoted string |
The difference between single and double-quoted strings is that variables and certain special escape codes are interpolated into double quoted strings, but not in single-quoted ones. Here are some escape codes:
| \n | New line |
|---|---|
| \t | Tab |
| \r | Carriage return |
| \f | Form feed |
| \a | Ring bell |
| \040 | Octal character (octal 040 is the space character) |
| \0x2a | Hexadecimal character (hex 2A is the "*" character) |
| \cA | Control character (This is the ^A character) |
| \u | Uppercase next character |
| \l | Lowercase next character |
| \U | Uppercase everything until \E |
| \L | Lowercase everything until \E |
| \Q | Quote non-word characters until \E |
| \E | End \U, \L or \Q operation |
"Here goes\n\tnothing!"; # evaluates to: # Here goes # nothing! 'Here goes\n\tnothing!'; # evaluates to: # Here goes\n\tnothing! "Here goes \unothing!"; # evaluates to: # Here goes Nothing! "Here \Ugoes nothing\E"; # evaluates to: # Here GOES NOTHING! "Alert! \a\a\a"; # evaluates to: # Alert! (ding! ding! ding!) |
Putting backslashes in strings is a problem because they get interpreted as escape sequences. To inclue a literal backslash in a string, double it:
"My file is in C:\\Program Files\\Accessories\\wordpad.exe"; # evaluates to: C:\Program Files\Accessories\wordpad.exe |
Put a backslash in front of a quote character in order to make the quote character part of the string:
"She cried \"Oh dear! The parakeet has flown the coop!\""; # evaluates to: She cried "Oh dear! The parakeet has flown the coop!" |
You can refer to numeric values using integers, floating point numbers, scientific notation, hexadecimal notation, and octal. With some help from the Math::Complex module, you can refer to complex numbers as well:
123; # an integer 1.23; # a floating point number -1.23; # a negative floating point number 1_000_000; # you can use _ to improve readability 1.23E45; # scientific notation 0x7b; # hexadecimal notation (decimal 123) 0173; # octal notation (decimal 123) use Math::Complex; # bring in the Math::Complex module 12+3*i; # complex number 12 + 3i |
You can also enclose a string in backtics (`). This has the unusual property of executing whatever is inside the string as a Unix system command, and returning its output:
`ls -l`; # evaluates to a string containing the output of running the # ls -l command |
The last type of literal that Perl recognizes is the list, which is multiple values strung together using the comma operator (,) and enclosed by parentheses. Lists are closely related to arrays, which we talk about later.
('one', 'two', 'three', 1, 2, 3, 4.2);
# this is 7-member list contains a mixure of strings, integers
# and floats |
|
| Contents |
Next |