Labels

Monday, July 25, 2011

Learning Perl Notes --1

The following few posts are the learning notes of "Learning Perl".

Perl data and variable types
  1. All Numbers Have the Same Format Internally, double-precision floating-point value.
  2. Unlike C or C++, there’s nothing special about the NUL character in Perl because Perl uses length counting, not a null byte, to determine the end of the string.
  3. Strings
  • Single-Quoted String
Any character other than a single quote or a
backslash (including newline characters, if the string continues onto successive lines) stands for itself inside a string. Only when the backslash is followed by another backslash or a single quote does it have special meaning.
To get a backslash, put two backslashes in a row, and to get a single quote, put a backslash followed by a single quote.
\' and \\
  • double-Quoted String
the backslash takes on its full power to specify certain control characters, or even any character at all through octal and hex representations.

variable interpolated, some variable names within the string are replaced with their current values when the strings are used.

4. String operator
  • .   "hello" . "world" # same as "helloworld"
  • string repetition operator:  lowercase letter x
    "fred" x 3   # is "fredfredfred"
    5 × 4  # is really "5" × 4, which is "5555"   
Automatic Conversion Between Numbers and Strings
all depends upon the operator being used on the
scalar value. If an operator expects a number (like + does), Perl will see the value as a
number. If an operator expects a string (like . does), Perl will see the value as a string.
So you just use the proper operators, and Perl will performs all the conversions for you.



Binary Assignment
a raise to the power of operator is written as **=. So, $fred **= 3 means “raise the number in $fred to the third power, placing the result back in $fred.”

Comparison Operators
For comparing strings, Perl has an equivalent set of string comparison operators, which
look like funny little words: lt le eq ge gt ne.


Boolean Values
If the value is a number, 0 means false; all other numbers mean true. how about negative one??
• 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.
There’s one trick hidden in those rules. Because the string '0' is the exact same scalar value as the number 0, Perl has to treat them both the same. That means that the string '0' is the only nonempty string that is false.

User Input
  • <STDIN> for user keyboard input

  • The chomp Operator
$text = "a line of text\n"; or from <STDIN>
chomp($text); # Gets rid of the newline character
Note: If a line ends with two or more newlines,* chomp removes only one. If there’s no newline,
it does nothing, and returns zero.

Undef and define function

The line-input operator <STDIN> is one operator that can return undef. 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 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:
$madonna = <STDIN>;
if ( defined($madonna) ) {
print "The input was $madonna";
}

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















No comments:

Post a Comment