1>: NumbersScalar is most often either a number or a string..
Internally, Perl computes both integers and floating-point numbers
with double-precision floating point values.
2+3
5.1 - 2.4
3 * 12
14 / 2
10.2 / 0.3
10 / 3
2 ** 3 = 8 (two to the third power)
2>: Strings
Strings are sequences of characters(like hello).
The shortest possible string has no characters.
The longest string fills all of your available memory..
Single-Quoted String
' ' # null string (no characters)
'hello\n' # backslash and n.
Double-Quoted String
"hello world\n"
"hello\tworld"
\l Lowercase next letter
\L Lowercase all following letters until \E
\u Uppercase next letter
\U Uppercase all following letters until \E
\Q 将到\E为止的所有非文字字符(non-word)加上反斜线
\E End \L, \U, or \Q
\n Newline
\r Return
\t Tab
\a Bell
\b Backspace
\e ESC
\cC Control-C
\\ Backslash
\" Double quote
String Operators
"hello" . "world" # same as "helloworld"
"hello" . ' ' . "world" # same as 'hello world'
'hello world' . "\n" # same as "hello world\n"
String repetition operator (left: a string right: a number)
"perl" x 3 # is "perlperlperl"
"ok" x (4+1) # is "okokokokok"
5 x 4 # is "5555" (the number 5 is converted to the string "5")
Automatic Conversion Between Numbers and Strings
"12" * "3" #gives the value 36
"12fred34" * " 3" #will also give 36
"Z" . 5 * 7 # same as "Z" . 35, or "Z35"
(just use the proper operators, and Perl will make it all work.)
3>: Perl's Built-in Warnings
$ perl -w my_program
or
#!/usr/local/bin/perl5.12.2 -w
or
#!/usr/local/bin/perl5.12.2
use warnings;
#!/usr/local/bin/perl5.12.2
use diagnostics;
(If you get a warning message you don’t understand, you can get a longer
description of the problem with the diagnostics pragma.)
Perl’s command-line options, -M, to
load the pragma only when needed instead of editing the source code each time to
enable and disable diagnostics:
$ perl -Mdiagnostics ./my_program
4>: Scalar Variables
a scalar variable can hold only one value. But other types of variables,
such as arrays and hashes,may hold many values..
$fred = $fred + 5; # without the binary assignment operator
$fred += 5; # with the binary assignment operator
$barney = $barney * 3;
$barney *= 3;
$str = $str . " "; # append a space to $str
$str .= " "; # same thing with assignment operator
$fred **= 3; # $fred = $fred ** 3;
5>: Output with print
print "The answer is ", 6*7,"\n"; # you can give print a series of values, separated by commas;
$barney = "fred ate a $meat"; # $barney is now "fred ate a"
'35' eq '35.0' # false (comparing as strings)
' ' gt ' ' # true
6>: The if Control Structure
the string '0' is the only nonempty string that is false.
7>:Getting User Input (The line-input operator )$line = ; # ( typically has a newline character on the end of it)
if ($line eq "\n") {
print " That was just a blank line!\n"
} else {
print " That line of input was: $line";
}
chomp($text = ); # read the text, without the newline character
$text = ; # do the same thing...
chomp($text); # ...but in two steps
As a function, it has a return value, which is the number of characters removed.
(If a line ends with two or more newlines, chomp removes only one. If there’s no newline,
it does nothing, and returns zero.)
8>: The while Control Structure
$count = 0;
while ($count < 10){
$count += 2;
print " count is now $count\n"; # gives values 2 4 6 8 10
}
9>: The undef Value
(If you try to use this “nothing” as a “numeric something,” it acts like
zero. If you try to use it as a “string something,” it acts like the empty string.)
# Add up some odd numbers
$n = 1;
while ($n < 10) {
$sum += $n;
$n += 2; # 1+3+5+7+9
}
print "The total was $sum.\n";
$string .= "more text\n";
10>: The defined Function
(Use the defined function, which returns false for undef, and true for everything else)
$madonna = ;
if (defined($madonna)){
print "The input was $madonna";
} else{
print "No input available!\n";
}
$madonna = undef; # As if it had never been touched (^_^)