Numeric literals are specified in any of the customary floating point or
0xffff # hex
0377 # octal
4_294_967_296 # underline for legibility
String literals are usually delimited by either single or double quotes.
They work much like shell quotes: double-quoted string literals are subject
to backslash and variable substitution; single-quoted strings are not
(except for ``
\''' and ``
\\''). The usual Unix backslash rules apply for making characters such as
newline, tab, etc., as well as some more exotic forms. See
Quote and Quotelike Operators for a list.
Octal or hex representations in string literals (e.g. '0xffff') are not
automatically converted to their integer representation. The
oct() functions make these conversions
for you. See hex and
oct for more details.
You can also embed newlines directly in your strings, i.e., they can end on
a different line than they begin. This is nice, but if you forget your
trailing quote, the error will not be reported until Perl finds another
line containing the quote character, which may be much further on in the
script. Variable substitution inside strings is limited to scalar
variables, arrays, and array slices. (In other words, names beginning with
$ or @, followed by an optional bracketed expression as a subscript.) The
following code segment prints out ``The price is $100.''
$Price = '$100'; # not interpreted
print "The price is $Price.\n"; # interpreted
As in some shells, you can put curly brackets around the name to delimit it
from following alphanumerics. In fact, an identifier within such curlies is
forced to be a string, as is any single identifier within a hash subscript.
Our earlier example,
can be written as
and the quotes will be assumed automatically. But anything more complicated
in the subscript will be interpreted as an expression.
Note that a single-quoted string must be separated from a preceding word by
a space, because single quote is a valid (though deprecated) character in a
variable name (see Packages).
Three special literals are
__PACKAGE__, which represent the current filename, line number, and package name at that point in your program. They may be used only as separate tokens; they will not be interpolated into strings. If there is no current package (due to a
__PACKAGE__ is the undefined value.
__DATA__ may be used to indicate the logical end of the script before the actual end of file. Any following text is ignored, but may be read via a
DATA filehandle: main::DATA for
PACKNAME is the current package) for
__DATA__. The two control characters
^Z are synonyms for
__DATA__ in a module). See
the SelfLoader manpage for more description of
__DATA__, and an example of its use. Note that you cannot read from the
DATA filehandle in a
BEGIN block: the
BEGIN block is executed as soon as it is seen (during compilation), at which point the corresponding
__END__) token has not yet been seen.
A word that has no other interpretation in the grammar
will be treated as if it were a quoted string. These are known as
``barewords''. As with filehandles and labels, a bareword that consists
entirely of lowercase letters risks conflict with future reserved words,
and if you use the -w switch, Perl will warn you about any such words. Some people may wish to
outlaw barewords entirely. If you say
use strict 'subs';
then any bareword that would
NOT be interpreted as a subroutine call produces a
compile-time error instead. The restriction lasts to the end of the
enclosing block. An inner block may countermand this by saying
no strict 'subs'.
Array variables are interpolated into double-quoted strings by joining all
the elements of the array with the delimiter specified in the
$LIST_SEPARATOR in English), space by default. The following are equivalent:
$temp = join($",@ARGV);
system "echo $temp";
system "echo @ARGV";
Within search patterns (which also undergo double-quotish substitution)
there is a bad ambiguity: Is
/$foo[bar]/ to be interpreted as
[bar] is a character class for the regular expression) or as
[bar] is the subscript to array @foo)? If
@foo doesn't otherwise
exist, then it's obviously a character class. If
Perl takes a good guess about
[bar], and is almost always right. If it does guess wrong, or if you're just
plain paranoid, you can force the correct interpretation with curly
brackets as above.
A line-oriented form of quoting is based on the shell
``here-doc'' syntax. Following a
<< you specify a string to terminate the quoted material, and all lines
following the current line down to the terminating string are the value of
the item. The terminating string may be either an identifier (a word), or
some quoted text. If quoted, the type of quotes you use determines the
treatment of the text, just as in regular quoting. An unquoted identifier
works like double quotes. There must be no space between the
<< and the identifier. (If you put a space it will be treated as a null
identifier, which is valid, and matches the first empty line.) The
terminating string must appear by itself (unquoted and with no surrounding
whitespace) on the terminating line.
The price is $Price.
print <<"EOF"; # same as above
The price is $Price.
print <<`EOC`; # execute commands
echo hi there
echo lo there
print <<"foo", <<"bar"; # you can stack them
I said foo.
I said bar.
myfunc(<<"THIS", 23, <<'THAT');
Here's a line
and here's another.
Just don't forget that you have to put a semicolon on the end to finish the
statement, as Perl doesn't know you're not going to try to do this:
Source: Perl data types
Copyright: Larry Wall, et al.