`

Note of Learning Perl--Scalar Data

    博客分类:
  • Perl
阅读更多

Scalar Data
--------------

1. Numbers
  1) Perl allows underscores for clarity within integer literals, so we can also write 61298040283768 like this: 61_298_040_283_768
  2) Nondecimal Integer Literals: Octal (base 8) literals start with a leading 0. The "leading zero" indicator works only for literals -- not for automatic string-to-number conversion.
  3) Perl also supports a modulus operator (%). The value of the expression 10 % 3 is the remainder when ten is divided by three, which is one. Both values are first reduced to their integer values, so 10.5 % 3.2 is computed as 10 % 3
  4) The result of a modulus operator when a negative number (or two) is involved can vary between Perl implementations. Beware.
  5) The exponentiation operator is represented by the double asterisk, such as 2**3, which is two to the third power, or eight.
  6) You can't normally raise a negative number to a noninteger exponent. Math geeks know that the result would be a complex number. To make that possible, you'll need the help of the Math::Complex module.
 
2. String
  1) The \n within a single-quoted string is not interpreted as a newline, but as the two characters backslash and n. Only when the backslash is followed by another backslash or a single quote does it have special meaning.
  2) The string repetition operator(x) wants a string for a left operand, so if the left operand is a number, the number will be converted to a string. The copy count (the right operand) is first truncated to an integer value (4.8 becomes 4) before being used. A copy count of less than one results in an empty (zero-length) string.
  3) Perl automatically converts the string to its equivalent numeric value, as if it had been entered as a decimal floating-point value.[50] So "12" * "3" gives the value 36. Trailing nonnumber stuff and leading whitespace are discarded, so "12fred34" * " 3" will also give 36 without any complaints.[51] At the extreme end of this, something that isn't a number at all converts to zero. This would happen if you used the string "fred" as a number.
  4) Automatic Conversion Between Numbers and Strings: The trick of using a leading zero to mean a nondecimal value works for literals, but never for automatic conversion. Use hex( )or oct( )to convert those kinds of strings.(See 1. Numbers 2)).
 
3. Scalar Variables
  1) Perl identifier is case sensitive.
  2) Variable interpolation is also known as double-quote interpolation, because it happens when double-quote marks (but not single quotes) are used.
    e.g:
      $fred = 'hello';
      print "The name is \$fred.\n";      #prints a dollar sign.
      print 'The name is $fred' . "\n";   #so does this.
  3) The variable name will be the longest possible variable name that makes sense at that part of the string. This can be a problem if you want to follow the replaced value immediately with some constant text that begins with a letter, digit, or underscore.[59] As Perl scans for variable names, it would consider those characters to be additional name characters, which is not what you want. Perl provides a delimiter for the variable name in a manner similar to the shell. Simply enclose the name of the variable in a pair of curly braces. Or, you can end that part of the string and start another part of the string with a concatenation operator:
    $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
    There are some other characters that may be a problem as well. If you need a left square bracket or a left curly brace just after a scalar variable's name, precede it with a backslash. You may also do that if the variable's name is followed by an apostrophe or a pair of colons, or you could use the curly-brace method described above.
   
4. The if Control Structure
  1) You may actually use any scalar value as the conditional of the if control structure.
  2) Perl doesn't have a separate Boolean data type, like some languages have. Instead, it uses a few simple rules:
    a. The special value undef is false.
    b. Zero is false; all other numbers are true.
    c. The empty string ('') is false; all other strings are normally true.
    d. The one exception: since numbers and strings are equivalent, the string form of zero, '0', has the same value as its numeric form: false.
    So, if your scalar value is undef, 0, '', or '0', it's false. All other scalars are true.
   
5. Getting User Input
  1) Use the line-input operator <STDIN> to get a value from standard input.
  2) The string value of <STDIN> typically has a newline character on the end of it.
  3) If you don't want to keep the newline character, you can use chomp operator.
  4) In fact, there's an easier way to use chomp, because of a simple rule: any time that you need a variable in Perl, you can use an assignment instead. So the most common use of chomp looks like this: chomp($text = <STDIN>).
  5) Chomp is actually a function. As a function, it has a return value, which is the number of characters removed.
  6) If a line ends with two or more newlines, chomp removes only one. If there's no newline, it does nothing, and returns zero.
  7) You may write chomp with or without the parentheses. This is another general rule in Perl: except in cases where it changes the meaning to remove them, parentheses are always optional.
 
6. The defined Function
  1) One operator that can return undef is the line-input operator, <STDIN>. Normally, it will return a line of text. But if there is no more input, such as at end-of-file, it returns undef to signal this.
  2) To tell whether a value is undef and not the empty string, use the defined function, which returns false for undef, and true for everything else.
  3) If you'd like to make your own undef values, you can use the obscurely named undef operator: $madonna = undef; # As if it had never been touched.
 
 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics