Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!sdd.hp.com!spool.mu.edu!uunet!rbj From: rbj@uunet.UU.NET (Root Boy Jim) Newsgroups: comp.lang.perl Subject: Re: Reading a file into a string Message-ID: <120644@uunet.UU.NET> Date: 31 Jan 91 00:02:39 GMT References: <97130172@bfmny0.BFM.COM> <11235@jpl-devvax.JPL.NASA.GOV> <97130173@bfmny0.BFM.COM> Organization: UUNET Communications Services, Falls Church, VA Lines: 40 In article <97130173@bfmny0.BFM.COM> tneff@bfmny0.BFM.COM (Tom Neff) writes: >In article <11235@jpl-devvax.JPL.NASA.GOV> lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes: >|In article <97130172@bfmny0.BFM.COM> tneff@bfmny0.BFM.COM (Tom Neff) writes: >|: In article <1991Jan30.005821.13013@convex.com> tchrist@convex.COM (Tom Christiansen) writes: The question is how to slurp a string into a file. Er, vice versa. LW>|You sure can, including the size of the file: >| >|4) open(FILE, $file); read(FILE, $foo, -s $file); >| >|or better in two ways, >| >|5) open(FILE, $file); sysread(FILE, $foo, -s FILE); >| >|I sincerely doubt that any other construct is going to beat that, >|timewise. ... > TN>However, 4) and 5) don't work on pipes, named or unnamed. 1) works on >named pipes but not unnamed. Only 2) and 3) work everywhere. OK, so Larry was being too literal. Suppose we replace -s FILE with $MAXINT? Sysread will get whatever it can. However, this begs the question, how does $foo get allocated? By sending the third arg to malloc? In that case, perhaps one should set a limit on how much they slurp and die/complain on therwise. This would also be a good time to ask about args to ioctls. I have been precreating them out of paranoia: $sgtty = 'ioekfl'; ioctl(FH,$TIOCGETP,$sgtty); Is this necessary? In either case, why? (6) $"=''; @str = ; $str = "@str"; (7) @str = ; $str = join(//,"@str"); Horshoe mode .signature: perl -e 'print "Just another $0 hacker,"' -- Root Boy Jim Cottrell Close the gap of the dark year in between