Learning Perl 4ed Reading Notes-Chapter2 scalar data

Source: Internet
Author: User
Tags perl interpreter
Learning Perl 4ed Reading Notes

Chapter2 scalar data
1. Numbers. All data (INT, float) is of the double type in Perl. Of course, sometimes Perl also uses integer to represent int internally, because this can improve the running speed of the program, but these logics are invisible for programmers, we don't have to worry about it.

2. Floating-point and integer literals. For example:
1.25
255.0
7.25e45
-12e-24

0
2001
-40

This is also possible: 61_344_3434_878, which is also an integer. It is opened purely in the underscore format for good readability. In fact, this is an integer. Why not use underscore with commas? This is because the comma is useful in Perl.

3. Non-decimal data. For example:

0377 -- octal, equivalent to decimal 255
0xff -- hexadecimal
0b111111 -- binary

Only literal (constant) can use characters such as 0x, 0b, and 0 to indicate non-decimal numbers. If it is a hexadecimal number stored in string, it is necessary to use a function to convert a string to a number. For example, you can use the OCT () function to convert "0377" to the octal number, and use hex () the function converts a string to a hexadecimal number. However, there is no conversion function for the binary number, which can be implemented using the OCT function. The OCT function can convert a string with a 0b header to a number.

4. numeric operators. addition, subtraction, multiplication, division, and division are certainly usable. In addition, like C, % Represents the remainder. Unlike Pascal and C, Perl supports ** operators to represent the exponent, for example, 2 ** 3 indicates the 3rd power of 2, which is unavailable to Pascal and C.

Note: When % is used to obtain the remainder, if either of the two operands is negative or both are negative, different Perl implementations (Perl interpreter) different answers may be provided, beware! 5. Strings. strings is a data type that can contain any character sequence. In addition to common ascii32 ~ Apart from the characters visible on these keyboards between ascii126, we can place any data, including binary data, in string. For example, you can put an image in a string.

Note: Unlike C, Perl uses the length of a string to determine the end position of a string, rather than \ 0 to determine whether the string ends.

6. Single-quoted string literals and double-quoted string literals. Similar to other languages, strings are enclosed in single quotes, with few items available. For example, the special syntax for referencing Perl variables in single quotes does not take effect only in strings contained in double quotes. For example, escape with backslash, in a single quotation mark string, only two characters can be escaped using backslash, namely \ 'and \. The former represents a single quotation mark, and the latter represents a backslash. In addition, the backslash character is used, it is actually a backslash character. For example, the string 'Hello \ n' indicates that "hello" is added with a backslash, and "N" is added at last, and "\ n" is not understood as a carriage return. The dual-cited number is much more free. You can escape many characters with backslash. For details, see Appendix 1.

7. String operators. Here we introduce two. The first one is the. symbol. Like PHP. (a single dot) concatenates two strings, such as "hello ". "world" indicates "helloworld"; the second operator is X (lowercase x characters), which indicates repetition. For example, "Fred" X3 indicates "fredfred ". The multiples of this X can be written as a float. Perl automatically truncates this float to an integer and then performs the operation. For example, "Fred" x4.8 indicates "Fred" X4. In addition, if this multiple is less than 1, we will get an empty (zero-length) string.

8. automatic conversion between numbers and strings. in most cases, Perl will automatically convert the type between string and number based on operator, for example, +. Only number can perform the + action, perl automatically converts all types to numbers, for example ., perl will automatically process the operation objects on both sides as strings (that is why Perl does not use ++ as a string connection ). Sometimes the automatic type conversion of Perl is amazing. For example, "12fred34" * 3, we get 36, that is, Perl will discard the latter when it finds it is not a number from the beginning, "12fred34" is treated as 12. If a string cannot be converted to a number, Perl converts it to 0 for processing. Try it yourself.

Learning Perl 4ed Reading Notes

Chapter2 scalar data
9. Perl's built-in warnings. This section is useful. First, we can use Perl-W my_perl_program to execute our Perl program to open all the warning information. Second, we can also write this in the first line of our Perl program :#! /Usr/bin/perl-W has the same effect. more effectively, use diagnostics is written into the Perl program. A line of code enables diagnostics, this is more detailed and comprehensive than the warning information. You can view more information in the perldiag man manual. However, every time you open or close the diagnostics information, you have to edit the Perl program source code, which is annoying, you can also open the diagnostics message in the command line, such as Perl-mdiagnostics my_perl_program. This effect is the same as writing use diagnostics in the code, but it is more free.

10. scalar variables. this section describes how to define variables in Perl. The $ symbol is used, which is the same as that used in shell programming. The difference is that the $ symbol is used to reference the value of a variable in shell programming, however, when assigning values to this variable, you do not need to use the $ symbol. Perl is different. In Perl, whether it is to assign values to the variable or to reference the value of the variable, you need to use the $ symbol. In fact, this is more in line with programming habits. For example:

$ Fred = 17; # give $ Fred the value of 17
$ Barney = 'hello'; # give $ Barney the five-character string 'hello'
$ Barney = $ Fred + 3; # give $ Barney the current value of $ Fred plus 3 (20)
$ Barney = $ Barney * 2; # $ Barney is now $ Barney multiplied by 2 (40)

11. Binary assignment operators. Perl is the same as C and supports the following syntax:

$ Fred + = 5;
$ Barney * = 3;
$ Str. = ""; # The result is $ STR = $ Str ."";
$ Fred ** = 3; # $ Fred = $ Fred ** 3;

12. output with print. Print this function can be used for printing in Perl. In fact, I found that Perl can be the same as C, and printf can be used to print things without print:

Print "the answer is ";
Print 6*7;
Print ". \ n ";

The above can also be written in this way. At this time, we find that the comma in Perl is used here:

Print "the answer is", 6*7, ". \ n ";

13. variables can be directly written into double quotes, so Perl will replace the variable value. Note that unspecified variables will be printed as empty strings, in addition, if Perl's warning is enabled, Perl will complain that we are referencing undefined variables. If we want to print a $ symbol, it is OK to use the backslash escape directly.

Like in shell programming, Perl supports the use of {} to enclose variables. This allows you to explicitly define which variable is used to prevent confusion:

$ What = "brontosaurus steak ";
$ N = 3;
Print "Fred ate $ N $ whats. \ n"; # Not the steaks, but the value of $ whats
Print "Fred ate $ N $ {What} s. \ n"; # Now uses $ what
Print "Fred ate $ N $ what". "S. \ n"; # another way to do it
Print 'fred ate'. $ n. ''. $ what." S. \ n "; # an especially difficult way

14. comparision operators. in Perl, the comparison operators used for number and string are different. For details, see Appendix 1. To compare two strings, use operator such as EQ and NE, rather than>, <these symbols. Perl compares strings with Char and Char. Note: upper-case letters are smaller than lower-case letters because in the ASCII code table, all uppercase letters are placed before lowercase letters. 15. if control structure. like C, the difference is that {} cannot be saved at any time. In C, if the if clause contains only one sentence, you can omit {}. Perl cannot.

Code: select all
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";
}

16. boolean value. Perl does not have any special bool data. Perl determines that bool depends on the following three items:

If the value is a number, 0 means false; all other numbers mean true.

If the value is a string, the empty string ('') means false; all other strings mean true.

If the value is another kind of scalar than a number or a string, convert it to a number or a string and try again. []

Note that the string "0" also indicates false, and the empty string also indicates false, which is a special case. After mastering the above three conditions, we can flexibly write bool conditions in Perl if.

17. Getting user input. Perl:

Code: select all
$line = <STDIN>;
if ($line eq "\n") {
  print "That was just a blank line!\n";
} else {
  print "That line of input was: $line";
}

If you want to obtain the input from a file, see chapter 5. In addition, note that the string read in this way will end with a newline (\ n), which is the same as C. To remove the \ n at the end of the line, use chomp.

18. Chomp. Chomp is a function used to remove \ n at the end of a row, as shown below:

Chomp ($ text = <stdin>); # read the text, without the newline character

$ Text = <stdin>; # Do the same thing...
Chomp ($ text); #... but in two steps

The first method is recommended, which is concise. It can also be written as follows: chomp $ text. In Perl, a function is often called without parentheses unless in some special cases. In addition, as a function, Chomp has return value, but Chomp's return value is generally not used. It indicates how many \ n chomp removed. Generally, this value is 1. Chomp removes only one \ n. If one row contains multiple \ n, Chomp removes only one. If the row does not contain \ n, Chomp does not do anything.

19. The while control structure. Check the code and click OK:

Code: select all
$count = 0;
while ($count < 10) {
  $count += 2;
  print "count is now $count\n"; # Gives values 2 4 6 8 10
}

Like if, {} cannot be omitted under any circumstances.

20. UNDEF value. as mentioned above, if a variable has not been assigned a value, its value is UNDEF. If it is a number, this UNDEF will be interpreted as 0, if it is a string, this UNDEF will be interpreted as an empty string. Sometimes we can use this feature to directly use a variable. In most cases, this is no problem. If we enable the Perl warning function, Perl prints the warning information when referencing an UNDEF variable.

21. defined function. We can use the defined function provided by Perl to test whether the value of a variable is UNDEF (instead of 0 or empty string ). Sometimes UNDEF may occur. For example, when an object is read, if the EOF is read, the variable will become the value of UNDEF:

Code: select all
$madonna = <STDIN>;
if ( defined($madonna) ) {
  print "The input was $madonna";
} else {
  print "No input available!\n";
}

We can also manually assign the UNDEF value to the variable. However, in this case, nothing is done.

$ Madonna = UNDEF; # as if it had never been touched

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.