Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!spool2.mu.edu!sdd.hp.com!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: While learning PERL... a suggestion Keywords: perl file learn suggest Message-ID: <11115@jpl-devvax.JPL.NASA.GOV> Date: 19 Jan 91 02:11:53 GMT References: <1991Jan19.003519.23569@ux1.cso.uiuc.edu> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 42 In article <1991Jan19.003519.23569@ux1.cso.uiuc.edu> phil@ux1.cso.uiuc.edu (Phil Howard KA9WGN) writes: : While learning PERL I have reached the part about the various file test : operator. I noted that -B and -T need to read SOME of the file to make : their appropriate determinations. I though of another kind of test that : could be useful, especially from pipes where the name might not be there. : : Suggestion: : : Add a -Z file test operator that returns TRUE if the file appears to be : the output of the UNIX compress command. Testing this file with -B would : still yield TRUE since a compressed file is a subset of binary files. I don't think Perl should have operators to check magic numbers. It's too easy to read the first word yourself now that there's sysread(). : I've still yet to get through the rest of PERL (probably this weekend) : so I don't know yet if it is particularly easy to invoke uncompress -c : or zcat to pipe input data back to a PERL script. When reading from : a file one can reposition back to the beginning of the file and pass : the file descriptor on to zcat as STDIN and read from the pipe instead. : But for something already coming in from a pipe, the suggested -Z test : would have already taken data out of the pipe. There's no way to do this at all under Unix, let alone Perl, without interposing a process to supply the magic number you destructively read out of the pipe. You can't seek backwards on a pipe, and unless you can convince compress to accept input without the leading magic number, you're stuck. As you pointed out, it wouldn't help to give Perl a -Z, since it would still have to read the pipe. There's one other possibility. If you know the pipe is really a socket, you might be able to do a recv() with the MSG_PEEK flag and read it out non-destructively. Likewise for streams, using I_PEEK. : Well I hope I don't have egg all over my face with this suggestion. : We shall see. I'm afraid the yolk is on all of us. Larry