[ previous ] [ Contents ] [ 1 ] [ 2 ] [ 3 ] [ next ]
The simplest kind of data in Perl are scalar data, that
can be mostly numbers or strings of characters.
Both integers and floating-point numbers have an internal double-precision floating point representation.
Examples of floating-point literals[1]
1.24
255.005
5.235E45
-25.0E-11
251.0
-1.9221
Examples of integer literals
24
5
0
-45
-2501
19221
2519988585883
2_519_988_585_883
2 + 4
5.6 - 34.4447
4.5 * 4
-4 / 3
55.5 / 3
75 % 2
70 % 3
Strings are character sequences that may contain any possible compination of characters. We may differentiate between single- and double-quoted string literals.
Single-Quoted
'Markus'
'Lena'
''
'Shannon'
'let\'s include an apostrophe!'
'and a backslash: \\'
'a backslash and n: \n'
Double-Quoted
In this case the backslash is used to specify certain control characters.
"Barbara"
"Ana"
"Hello Karen!\n"
"Black\tWhile"
The most important string backslash escapes are the following
\a Beep
\b Backspace
\c "Control" caracter. \cD = CTRL-D
\e Escape
\f Form feed
\l Make the next letter lowercase
\n New line, return.
\r Carriage return.
\t Tab.
\u Make the next letter uppercase
\x Enables hex numbers
\v Vertical tab
\\ Print backslash
\" Print double quotes
\ Escape next character if known otherwise print. Also allows octal numbers.
\L Make all letters lowercase until the \E
\U Make all letters uppercase until the \E
\Q Add a backslash-quote to all the nonalphanumerics until the \E
\E Terminates the effects of \L, \U, or \Q
\007 Any octal ASCII value
\x7f Any hexadecimal value
\cx Control-x
"hello"."world"
"GNU"."/"."Linux"
"This is"." "."a sentence.\n"
"Tuxie " x 5
70 x 3
Perl performs the conversion between numbers and strings when it
is necessary.
Variables holding exactly one value that start with a $ (named the
sigil) followed by a Perl identifier. All the variables
that follow are different
$hello
$Hello
$HELLO
$Starting_Value
$quite_long_variable_name
It is important to select meaningful variable names, making use of underscores when possible[2].
The Perl assignment operator is the equals sign that takes a
variable name in the left side that takes the value of the expression on the
right.
$hello = 5;
$Hello = 4.33;
$HELLO = "Good morning!\n";
$Starting_Value = $index - 3;
$quite_long_variable_name = $x * 2;
Binary assigments are shortcuts like the following
$a = $a + 3;
$a += 3;
$a = $a * 3;
$a *= 3;
$string = $string." ";
$string .= " ";
print "This is a message for you.\n";
print "This is ";
print "a message for you.";
print "\n";
print "This is ", "a message for you.";
print "The solution is ", 2*3.125,"\n";
Scalar variables in doubly-quoted string literals are subject to variable interpolation
$op_sys = "GNU/Linux";
print "One of the best operating systems is $op_sys\n";
To print the dollar sign it has to be escaped or between single quotes
print "The \$op_sys variable value is $op_sys\n";
print 'The \$op_sys variable value ', "is $op_sys\n";
The variable name can be located between curly braces to prevent errors delimiting variable names
$job = "student";
print "The book\'s owner is an $student\n";
print "This is the favourite bar of the college ${job}s\n";
From the perlop documentation.
Associativity Precedence (highest to lowest)
left terms and list operators (leftward)
left ->
nonassoc ++ --
right **
right ! ~ \ and unary + and -
left =~ !~
left * / % x
left + - .
left << >>
nonassoc named unary operators
nonassoc < > <= >= lt gt le ge
nonassoc == != <=> eq ne cmp ~~
left &
left | ^
left &&
left || //
nonassoc .. ...
right ?:
right = += -= *= etc.
left , =>
nonassoc list operators (rightward)
right not
left and
left or xor
In case of doubt: use parenthesis...
Comparison operators return a true or false value and are the following
==
eq
!=
ne
<
lt
<=;
leq
>
gt
>=;
geq
<=>
comp
The unary not operator (!) give the opposite value of any Boolean value.
The if control structure defines a block that only executed if its associated condition returns a true value
if ($name gt "Monika") {
print "'$name' is after 'Monika' in sort order\n";
}
The keyword else allows an alternative choice
if ($name gt "Monika") {
print "'$name' is after 'Monika' in sort order\n";
} else {
print "'$name' is before 'Monika' in sort order\n";
}
You may use any scalar value in the conditional
$value_1 = 10.0;
$value_2 = 2;
$check = $value_1 > $value_2;
if ($check) {
print "\$value_1 is larger than \$value_2\n";
}
The rules for deciding if a value is true or false are the following:
All numbers are true except 0 (zero).
All strings are true besides the empty string ('').
All other cases are converted to a number or a string and the previous rules apply.
The simplest way to get a value from the keyboard into the program is the
line-input operator, <STDIN>. Every time a program finds an
<STDIN> where a scalar value is expected, Perl
reads the next complete line from the standard input. The newline
character at the end of the line can be removed using the chomp
operator.
$value_1 = 10.0;
$value_2 = <STDIN>;
print "\$value_2 = $value_2\n";
chomp($value_2); # newline is removed
print "\$value_2 = $value_2\n";
$check = $value_1 > $value_2;
if ($check) {
print "\$value_1 is larger than \$value_2\n";
}
This can be done in a single step
$value_1 = 10.0;
chomp($value_2 = <STDIN>);
print "\$value_2 = $value_2\n";
$check = $value_1 > $value_2;
if ($check) {
print "\$value_1 is larger than \$value_2\n";
}
This is one of the possible control structures in Perl. It
repeats a block of code as long as a given condition is accomplished:
$counter = 10;
while ($counter > 0) {
print "\$counter = $counter\n";
$counter -= 2;
}
The conditional is evaluated prior to the first iteration, thus it is possible that the block is not executed a single time if the condition is initially false.
Values used before being assigned take the special value undef. If it is expected to take a numerical value then the assigned value is zero, while in a string value the variable takes the empty string value.
This is a standard behavior, though Perl will usally warn the user
when unusual uses of the undef value occur.
[ previous ] [ Contents ] [ 1 ] [ 2 ] [ 3 ] [ next ]
Perl notes
$Id: perl_notes.sgml,v 1.3 2011/12/12 15:41:58 curro Exp curro $francisco.perez@dfaie.uhu.es