Learning Perl: 5.1. Input from Standard Input

Previous Page
Next Page

 

5.1. Input from Standard Input

Reading from the standard input stream is easy. We've been doing it with the <STDIN> operator.[*] Evaluating this operator in a scalar context gives you the next line of input:

[*] What we're calling the line-input operator here, <STDIN> is actually a line-input operator (represented by the angle brackets) around a filehandle. You'll learn about filehandles later in this chapter.

    $line = <STDIN>;                # read the next line
    chomp($line);                   # and chomp it

    chomp($line = <STDIN>);         # same thing, more idiomatically

Since the line-input operator will return undef when you reach end-of-file, this is handy for dropping out of loops:

    while (defined($line = <STDIN>)) {
      print "I saw $line";
    }

There's a lot going on in that first line: we're reading the input into a variable, checking that it's defined, and if it is (meaning that we haven't reached the end of the input), we're running the body of the while loop. So, inside the body of the loop, we'll see each line, one after another, in $line.[*] This is something you'll want to do fairly often, so naturally Perl has a shortcut for it. The shortcut looks like this:

[*] You probably noticed that we never chomped that input. In this kind of a loop, you can't put chomp into the conditional expression, so it's often the first item in the loop body when it's needed. We'll see examples of that in the next section.

    while (<STDIN>) {
      print "I saw $_";
    }

Now, to make this shortcut, Larry chose some useless syntax. That is, this is saying, "Read a line of input, and see if it's true. (Normally it is.) And if it is true, enter the while loop, but throw away that line of input!" Larry knew that it was a useless thing to do; nobody should ever need to do that in a real Perl program. So, Larry took this useless syntax and made it useful.

What this is actually saying is that Perl should do the same thing as we saw in our earlier loop: it tells Perl to read the input into a variable, and (as long as the result was defined, so we haven't reached end-of file) then enter the while loop. However, instead of storing the input into $line, Perl will use its favorite default variable, $_, as if you had written this:

    while (defined($_ = <STDIN>)) {
      print "I saw $_";
    }

Now, before we go any further, we must be clear about something: this shortcut workparticular, as a statement all on its own) it won't read a line into $_ by default. It works only if there's nothing but the line-input operator in the conditional of a while loop.[] If you put anything else into the conditional expression, this shortcut won't apply.

[] Well, okay, the conditional of a for loop is just a while conditional in disguise, so it works there, too.

Otherwise, there's no other connection between the line-input operator (<STDIN>) and Perl's favorite default variable ($_). In this case, the input is being stored in that variable.

On the other hand, evaluating the line-input operator in a list context gives you all of the (remaining) lines of input as a list, and each element of the list is one line:

    foreach (<STDIN>) {
      print "I saw $_";
    }

Once again, there's no connection between the line-input operator and Perl's favorite default variable. In this case, though, the default control variable for foreach is $_. So this loop places each line of input in $_, one after the other.

That may sound familiar and for good reason: that's the same behavior as the while loop, isn't it?

The difference is under the hood. In the while loop, Perl reads a line of input, puts it into a variable, and runs the body of the loop. Then, it goes back to find another line of input. But in the foreach loop, the line-input operator is being used in a list context since foreach needs a list to iterate through. So, it has to read all of the input before the loop can start running. That difference will become apparent when the input is coming from your 400MB web server log file. It's generally best to use code like the while loop's shortcut whenever possible, since it processes input one line at a time.

Previous Page
Next Page
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值