Learning Perl: 2.8. The chomp Operator
来源:互联网 发布:京东和淘宝买吉他 编辑:程序博客网 时间:2024/06/15 05:25
![Previous Page](http://writeblog.csdn.net/images/prev.gif)
![Next Page](http://writeblog.csdn.net/images/next.gif)
2.8. The chomp Operator
The first time you read about the chomp operator, it seems overspecialized. It works on a variable, and the variable has to hold a string. If the string ends in a newline character, chomp can get rid of the newline. That's (nearly) all it does as in this example:
$text = "a line of text/n"; # Or the same thing from <STDIN> chomp($text); # Gets rid of the newline character
It turns out to be so useful, you'll put it into nearly every program you write. As you see, it's the best way to remove a trailing newline from a string in a variable. In fact, there's an easier way to use chomp because of a simple rule: whenever you need a variable in Perl, you can use an assignment instead. Perl does the assignment and then it uses the variable in whatever way you requested. The most common use of chomp looks like this:
chomp($text = <STDIN>); # Read the text, without the newline character $text = <STDIN>; # Do the same thing... chomp($text); # ...but in two steps
At first glance, the combined chomp may not seem to be the easy way, especially if it seems more complex. If you think of it as two operations, read a line and chomp it, then it's more natural to write it as two statements. If you think of it as one operation, read just the text and not the newline, it's more natural to write the one statement. Since most other Perl programmers are going to write it that way, you may as well get used to it now.
chomp is a function. As a function, it has a return value, which is the number of characters removed. This number is hardly ever useful:
$food = <STDIN>; $betty = chomp $food; # gets the value 1 - but you knew that!
As you see, 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.
If a line ends with two or more newlines,[*] chomp removes only one. If there's no newline, it does nothing and returns zero.
[*] This situation can't arise if you're reading a line at a time, but it can when you have set the input separator ($/) to something other than newline, use the read function, or perhaps have glued some strings together yourself.
![](http://writeblog.csdn.net/images/pixel.gif)
![Previous Page](http://writeblog.csdn.net/images/prev.gif)
![Next Page](http://writeblog.csdn.net/images/next.gif)
- Learning Perl: 2.8. The chomp Operator
- Learning Perl: 4.9. The return Operator
- Learning Perl: 8.4. The Binding Operator, =~
- Learning Perl: 9.2. The split Operator
- Learning Perl: 5.2. Input from the Diamond Operator
- Perl chomp
- perl the special operator
- The "tr///" operator in Perl
- Perl chomp 操作
- Perl chomp使用
- 2 Perl - chomp 操作
- [Perl]chomp 和 $/
- Perl chomp使用
- Perl 之 chomp 用法
- chop() vs. chomp() of Perl
- perl中chomp的使用
- Perl 的 chomp 命令用法
- Perl中chomp和chop
- hdu 3463Goldbach Division(打表法)
- fusionChart的使用
- 用 win32 API 模拟按键
- Perl Learning: 2.6. The if Control Structure
- Learning Perl: 2.7. Getting User Input
- Learning Perl: 2.8. The chomp Operator
- 笔记
- C++ CFile::Rename 出现“拒绝访问”异常 及解决方案(不能说原创,自己悟出来的)
- JAVA的内省机制(introspector)与反射机制(reflection)
- 无电池的情况下,如何升级DELL INSPIRON 6400的bios
- Perl Learning: 2.9. The while Control Structure
- Perl Learning: 2.10. The undef Value
- Learning Perl: 2.11. The defined Function
- A