“Perl language entry” self-Notes - Chapter 2
Chapter II scalar data
Perl scalar said in the scope of great, including figures and characters / strings. And the figures are unlikely to c in the float, single-refined, double-refined, the integer … points, as long as it is the scalar figures. And all its internal digital format are the same, perl integer will be preserved for the float, is calculated in accordance with the floating-point calculation.
Figures that: (in this respect can be seen perl randomness)
1.25
255.000
7.25e45
- 6.5e24
- 12e-24
- 1.2E-23
0
-40
61298040283768
61_298_040_283_768 (this figure and said that the above method is the same, but in real life is with a comma, and is used here _. Comma because there has been a more important role)
0377 (octal 377)
0xff (hexadecimal the FF)
Numerical operators:
2 +3 (2 + 3 = 5, we could also add spaces plus about. Perl very relaxed requirements in this regard, the same below)
5.1-2.4 (5.1 by 2.4 = 2.7)
3 * 12
14 / 2
10.2/0.3
10 / 3
10% (from MS, needs to be pointed out is that if the two values are minority, perl the way into first decimal integer, such as 10.5% 3.2 accordance with the terms of 10% 3)
2 × × 3 (2 3rd power)
Single quotes string: single quotation marks are not part of the string, perl used to determine the beginning and end of the string. There is the actual meaning of characters, unlike a lot of double quotes string escaped. But if in a single quotation marks to express single quotes and backslash how to add a backslash in front.
'Fred'
''
'Hello \ n' (here \ n Enter newline not only two characters)
'\' \ \ '(A single quotes, a backslash)
Double quotes strings: will be able to explain many escaped characters (see table below), and double quotes in the interpolation can be variable.
"Barney" ( 'barney' the same meaning)
"Hello world \ n"
Written meaning \ n newline \ r Enter \ t tabs \ f-page \ b backspace at \ a ringing \ e Esc \ 007 arbitrary ASCII code octal value (= 007 ringing here) \ x7f arbitrary ASCII code hexadecimal values (f = 7 deleted here) \ cC arbitrary Ctrl key combination characters (this is Ctrl-C) \ \ backslash \ "double quotes \ l under a lowercase letters \ L after all lowercase letters Until \ E \ u capital letters under a \ U behind all capital letters until \ E \ Q Add backslash behind the non-quoted words of characters until \ E \ E end \ L \ U or \ Q
String operator: "." And "x"
. Operator is used to connect the string
"Hello". "World" (with "helloworld")
"Hello." ''. "World" (with "hello world")
X operator called string repeat operator, followed by figures represent the number of repeat
"Fred" x 3 (fredfredfred)
5 x 4 (5555, Attention, here is an integer can only repeat a string operation, if less than an equivalent space)
String figures and the automatic conversion:
In most cases, Perl based operators to automatically determine the number or string is the target. Perl deal with this aspect of the very intelligent, you can open perl warning switch, so if you use inappropriate, perl will prompt you. That is, in the first chapter, I say - w switch, perl procedures used in the first line.
Scalar variables:
Perl variables is the beginning of the $ dollar sign, beginning with a letter or underscore, and the case!
Binary assignment operator:
$ Fred = 5 + (equivalent to $ fred = $ fred + 5)
Fred *= $ 3 (equivalent to $ fred = $ fred * 3)
$ Str .= "" (equivalent to $ str = $ str. "The")
Priority:
Perl of the same priority and C ^ _ ^
Comparison operators:
Table or in a bar, seem clear:
Comparison of numerical strings are equal == eq not equal! = Ne less than <lt greater than> gt less than or equal to <= le than equivalent> = ge
Tip two:
- Shell and the way the contrary, most people here still fairly accustomed to the treatment.
- How memory string keyword: Actually, the words are written in English, as long as understood what the meaning of the ok. As follows (not necessarily accurate ^ _ ^, convenient memory like): eq = equal, ne = not equal, less than lt =, = gt great than, less than le =, = great than ge
If control structure:
Examples to illustrate the structure of if statement:
If ($ name gt 'fred') (
Print " '$ name' comes after 'fred' in sorted order. \ N";
)
This is the most simple if statement, no else paragraph. Below again on the basis of the above else:
If ($ name gt 'fred') (
Print " '$ name' comes after 'fred' in sorted order. \ N";
Else ()
Print " '$ name' does not come after 'fred' \ n";
Print "Maybe it's the same string, in fact. \ N";
)
See here I have a question. These two examples are copied from the book, according to my previous results should be read in the print statement in the name should not be coupled with $ single quotes, so we can really show that the contents of variables. If so, not a single quotation marks on behalf of $ name? Tests done immediately known: in this case before I add $ name = "marco";
However, the results showed that:
Bash-2.05 $. / Test.plx
'Marco' comes after 'fred' in sorted order.
Sheets can be explained in quotation marks or variable content. However, I continue to test single quotes really can be explained variables, it is simple to write a few words:
#! / Usr / local / bin / perl-w
$ Named = "marco";
Print '$ named';
Results indeed the error message:
Bash-2.05 $. / Test.plx
Name "main:: named" used only once: possible typo at. / Test.plx line 2.
If the two sides in a single quotation marks with a double quotes on ok. Output results are as follows:
'Marco'
If the original single quotes in double quotes inside, the single quotes is the single quotes, there is no other significance. Ok, see ^ _ ^
How Boolean value of true or judgement?
If scalar value for undef, 0,''or'0 ', then it is a fake. All other really.
Obtain user input:
There should be many ways, but now first introduced this <STDIN> operator. Like, for example to illustrate:
$ Line = <STDIN>;
If ($ line eq "\ n") (
Print "That was just a blank line! \ N";
Else ()
Print "That line of input was: $ line";
)
It is worth noting that due to <STDIN> by the end of the round as. However, it will also be Enter newline Add variable. Hence the need chomp on the operation of this very useful at the ^ _ ^.
Chomp operators:
Really is a very useful parameter. Almost every procedure will be used. It is actually a function. Its role is removed at the end of a newline, if there are two or two more than it should use the same figures chomp.
Or that the most commonly used method is the use of the coolest:
Chomp ($ test = <STDIN>);
Good job of use is:
$ Test = <STDIN>;
Chomp ($ test);
A recommendation to use this only looks like a Perl programmer ^ _ ^.
While control structure:
Examples:
$ Count = 0;
While ($ count <10) (
$ Count + = 1;
Print "count is now $ count \ n";
)
No more explanation, I believe the Earth's people are able to see the procedure ^ _ ^.
Undef value:
Variables in the first assignment before its value is undef. If the use of digital undef as its functional equivalent of 0, so it can easily generate a numerical began to air accumulator:
# Cumulative some odd:
$ N = 1;
While ($ n <10) (
$ Sum + = $ n;
$ N + = 2;
)
Print "The total was $ sum \ n";
$ Sum in this procedure did not specify a start value, it is the beginning undef, the additive because it is digital, so it is used as a 0.
When this variable to be used as character, its role and empty characters similar.
Defined function:
Is designed to detect whether a variable as a function of undef examples:
$ = <STDIN> Madonna;
If (defined ($ madonna)) (
Print "The input was $ madonna";
Else ()
Print "No input available! \ N";
)
Tags: perl








0 Comments to ““Perl language entry” self-Notes - Chapter 2”
No Comments. Send your comment.
Leave a Reply
You must be logged in to post a comment.