home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / perl / 5913 < prev    next >
Encoding:
Internet Message Format  |  1992-09-13  |  3.5 KB

  1. Path: sparky!uunet!usc!wupost!micro-heart-of-gold.mit.edu!eddie.mit.edu!minya!jc
  2. From: jc@minya.UUCP (John Chambers)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: "return <STDIN>;" doesn't work
  5. Message-ID: <1364@minya.UUCP>
  6. Date: 14 Sep 92 02:07:41 GMT
  7. References: <1355@minya.UUCP> <1992Sep11.182522.23320@netlabs.com>
  8. Lines: 71
  9.  
  10. In article <1992Sep11.182522.23320@netlabs.com>, lwall@netlabs.com (Larry Wall) writes:
  11. > In article <1355@minya.UUCP> jc@minya.UUCP (John Chambers) writes:
  12. > :    for ($l = &getline()) ...
  13. > : I simplified the subroutine to:
  14. > :    sub getline {
  15. > :       return <STDIN>;
  16. > :    }
  17. > : and it returned a null value.  To find out what the @*&*!$^@ was
  18. > : going on, I rewrote it as:
  19. > : 
  20. > Note that the book says "return LIST".  That puts <STDIN> into a list
  21. > context, so <STDIN> is going to return The Whole Thing.  You need to say
  22. >    return scalar <STDIN>;
  23. > I'm a little surprised that you say it returned a  null  value.   It
  24. > should have returned the last line of the file. The last line wasn't
  25. > blank, was it?
  26.  
  27. Well, I don't think so, but it was a while ago, and that  part  of  my
  28. memory  has  since been freed and alloced for other uses.  I guess the
  29. confusion, which I haven't  been  able  to  quite  get  straight  from
  30. reading the camel book, is something like: I didn't quite pick up that
  31. the "return LIST" meant that return imposes a list context on its arg.
  32. I  had understood it to mean that return accepts a list as an arg, but
  33. this seems to me to not mean quite the same thing.  After all,  the  =
  34. operator  will  accept a list as either arg, and yet it doesn't impose
  35. array context on all its args.  So  it  seems  clear  that  there  are
  36. functions  in  perl  that  will accept either scalor or list args, and
  37. evaluate them in either context.  This  idea  was  reinforced  by  the
  38. warnings  about the scalar/list context being inherited from outside a
  39. function call.  It somehow  seemed  that  return  should  inherit  the
  40. calling  context  if  anything  in  a function did, and since I hadn't
  41. picked up on the idea that saying LIST in a function  prototype  meant
  42. an imposition of list context (just the willingness of the function to
  43. accept a list if offered one ;-), I wasn't expecting the list  context
  44. here.
  45.  
  46. Jeez, did any of that make sense?
  47.  
  48. Anyhow, I'll reread chapter four with the new understanding that  LIST
  49. is code for the imposition of list context, and maybe I won't make the
  50. same mistake in the future.  I'm sure that  I  still  have  unutilized
  51. mistakes  that  I  can  make  instead.   I'm  not  at  all sure that I
  52. understand what the rules are for passing along the context, so  there
  53. are likely still some mistakes to be made there..
  54.  
  55. Another thing I discovered that surprised  me  was  that  when  I  got
  56. around to writing the routine
  57.     sub getline {
  58.        <STDIN>;
  59.     }
  60. it  behaved  differently  than  the  one with return in it.  I've used
  61. several languages in which the last expression in a  function  is  the
  62. return  value (and I was somewhat disappointed with C for not doing it
  63. this way ;-).  In all of  them  that  I've  seen  so  far,  the  final
  64. statements:
  65.     x
  66. and
  67.     return x
  68. are  equivalent.   It's  a  bit surprising that this isn't the case is
  69. perl.  Was this a conscious  design  decision  (presumably  with  some
  70. reason  that I can't fathom), or did it just sort of happen?  Is there
  71. some clever use of this definition of return?
  72.  
  73. -- 
  74. All opinions Copyright (c) 1992 by John Chambers. Inquire for licensing at:
  75. 1-617-647-1813 ...!{bu.edu,harvard.edu,eddie.mit.edu,ruby.ora.com}!minya!jc 
  76. --
  77. Pensu tutmonde; agu loke.
  78.