home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!wupost!usc!news!netlabs!lwall
- From: lwall@netlabs.com (Larry Wall)
- Newsgroups: comp.lang.perl
- Subject: Re: "return <STDIN>;" doesn't work
- Message-ID: <1992Sep11.182522.23320@netlabs.com>
- Date: 11 Sep 92 18:25:22 GMT
- References: <1355@minya.UUCP>
- Sender: news@netlabs.com
- Organization: NetLabs, Inc.
- Lines: 41
- Nntp-Posting-Host: scalpel.netlabs.com
-
- In article <1355@minya.UUCP> jc@minya.UUCP (John Chambers) writes:
- : Well, here's one that probably means I'll learn just a little more
- : about perl. I had a routine that was giving nulls when I thought it
- : shouldn't, to a caller that looked like:
- :
- : for ($l = &getline()) ...
- :
- : I simplified the subroutine to:
- :
- : #Example_1:
- : sub getline {
- : return <STDIN>;
- : }
- :
- : and it returned a null value. To find out what the @*&*!$^@ was
- : going on, I rewrote it as:
- :
- : #Example_2:
- : sub getline {
- : local($x);
- : $x = <STDIN>;
- : return $x;
- : }
- :
- : To my surprise, this works! It delivers the data from standard input,
- : one line at a time, and a null at EOF. I've tried digging through the
- : camel book for a possible explanation why Example_2 should behave
- : differently from Example_1, and I don't understand it at all. Can
- : someone enlighten me? I suspect that there's something important here
- : that I haven't yet learned.
-
- Note that the book says "return LIST". That puts <STDIN> into a list
- context, so <STDIN> is going to return The Whole Thing. You need to say
-
- return scalar <STDIN>;
-
- I'm a little surprised that you say it returned a null value. It
- should have returned the last line of the file. The last line wasn't
- blank, was it?
-
- Larry
-