Wednesday, 30 October 2013

PS_02_Perl - Data_Types_&_Variables

Basic Types
The basic data types known to Perl are scalars, lists, and hashes. Scalar $foo Simple variables that can be a number, a string, or a reference. A scalar is a “thingy.” List @foo An ordered array of scalars accessed using a numeric subscript.
$foo[0]
Hash %foo An unordered set of key/value pairs accessed using the keys as subscripts. $foo{key}
Perl uses an internal type called a typeglob to hold an entire symbol table entry. The effect is that scalars, lists, hashes, and filehandles occupy separate namespaces (i.e., $foo[0] is not part of $foo or of %foo). The prefix of a typeglob is *, to indicate “all types.” Typeglobs are used in Perl programs to pass data types by reference. You will find references to literals and variables in the documentation. Literals are symbols that give an actual value, rather than represent possible values, as do variables.
For example in $foo = 1, $foo is a scalar variable and 1 is an integer literal.
Variables have a value of undef before they are defined (assigned). The upshot is that accessing values of a previously undefined variable will not (necessarily) raise an exception.

Variable Contexts
Perl data types can be treated in different ways depending on the context in which they are accessed. Scalar Accessing data items as scalar values. In the case of lists and hashes, $foo[0] and $foo{key}, respectively. Scalars also have numeric, string, and don’t-care contexts to cover situations in which conversions need to be done. List Treating lists and hashes as atomic objects Boolean Used in situations where an expression is evaluated as true or false. (Numeric: 0=false; String: null=false, Other: undef=false) Void Does not care (or want to care) about return value Interpolative Takes place inside quotes or things that act like quotes.

Special Variables (defaults)
Some variables have a predefined and special meaning to Perl. A few of the most commonly used ones are listed below.
$_ The default input and pattern-searching space
$0 Program name
$$ Current process ID
$! Current value of errno
@ARGV Array containing command-line arguments for the script
@INC The array containing the list of places to look for Perl scripts to
be evaluated by the do, require, or use constructs
%ENV The hash containing the current environment
%SIG The hash used to set signal handlers for various signals

Scalars
Scalars are simple variables that are either numbers or strings of characters. Scalar
variable names begin with a dollar sign followed by a letter, then possibly more letters,
digits, or underscores. Variable names are case-sensitive.

Numbers
Numbers are represented internally as either signed integers or double precision floating
point numbers. Floating point literals are the same used in C. Integer literals include
decimal (255), octal (0377), and hexadecimal (0xff) values.

Strings
Strings are simply sequences of characters. String literals are delimited by quotes:
Single quote ‘string’ Enclose a sequence of characters
Double quote “string” Subject to backslash and variable interpolation
Back quote `command` Evaluates to the output of the enclosed command
The backslash escapes are the same as those used in C:
\n Newline \e Escape
\r Carriage return \\ Backslash
\t Tab \” Double quote
\b Backspace \’ Single quote
In Windows, to represent a path, use either “c:\\temp” (an escaped backslash) or
c:/temp” (UNIX-style forward slash).
Strings can be concatenated using the “.” operator: $foo = “hello” . ”world”;

No comments :

Post a Comment