设为首页 收藏本站
查看: 596|回复: 0

[经验分享] Note of Learning Perl--Scalar Data

[复制链接]

尚未签到

发表于 2017-5-18 12:50:23 | 显示全部楼层 |阅读模式
  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.
 
 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-378814-1-1.html 上篇帖子: 用Perl进行数据加工处理 下篇帖子: Note of Learning Perl--Lists and Arrays
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表