In this language, we don't need to specify type of the data in your program, as Perl is loosly typed language. The Perl interpreter will manage the data type based on the context.
A variable can store numeric value or string
(a sequences of characters). Here, a string is alphanumeric value
enclosed in either single
'
or double
"
quotes.
Note that the double-quoted string literals allow variable
interpolation, and single-quoted strings do not. Like most of the
programming languages, it also supports escape sequence using \
symbol and special symbols like newline
\n
, tab
\t
etc. A few samples are shown in the following code.
There are three basic data types, these are scalar, array, and hash. These are discussed below:
Scalar | It's a simple variable (can be a number, a string, or a reference), which is preceded by $ symbol. |
---|---|
Array | It's an ordered list of scalars. The variable is preceded by @ symbol. |
Hash | It's an unordered set of key/value pairs. The hash variable is preceded by % symbol. |
In Perl same name can be used for variables of different data types as every variable type in maintained in separate namespace.
Scalar Variable
A scalar data might be an integer, a floating point, a character, a string, a paragraph, or content of a web page.
Array Variable
To refer to a single element of an array the
$
symbol is used with the variable name followed by the index of the
element in square brackets as shown in the following example.
Hash Variable
The following program shows how to work with key/value pairs.
Variable Context in Perl
Perl treats same variable differently based on Context as shown below.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.