📄 00000019.htm
字号:
command interpreter. Ask your Local Expert. </P> <BR> <BR> <P>Throughout this book, we'll just use <TT>#!/usr/bin/perl</TT> to represent <BR> all these notions and notations, but you'll know what we really mean by <BR> it. </P> <BR> <BR> <P>A random clue: when you write a test script, don't call your script <BR> <I>test</I>. UNIX systems have a built-in test command, which will likely <BR> be executed instead of your script. Try <I>try</I> instead. </P> <BR> <BR> <P>A not-so-random clue: while learning Perl, and even after you think <BR> you know what you're doing, we suggest using the <B>-w</B> option, especially <BR> during development. This option will turn on all sorts of useful and interesting <BR> warning messages, not necessarily in that order. You can put the <B>-w</B> <BR> switch on the shebang line, like this: </P> <BR> <BR> <PRE>#!/usr/bin/perl -w <BR> </PRE> <BR> <BR> <P>Now that you know how to run your own Perl program (not to be confused <BR> with the <I>perl</I> program), let's get back to our example. </P> <BR> <BR> <H2><A NAME="PERL2-CH-1-SECT-4"></A>1.4 Filehandles</H2> <BR> <BR> <P><A NAME="CH01.IOF1"></A><A NAME="CH01.IOF2"></A><A NAME="CH01.FH"></A>Unless <BR> you're using artificial intelligence to model a solipsistic philosopher, <BR> your program needs some way to communicate with the outside world. In lines <BR> 3 and 4 of our grade example you'll see the word <TT>GRADES</TT>, which <BR> exemplifies another of Perl's data types, the <I>filehandle</I>. A filehandle <BR> is just a name you give to a file, device, socket, or pipe to help you <BR> remember which one you're talking about, and to hide some of the complexities <BR> of buffering and such. (Internally, filehandles are similar to streams <BR> from a language like C++, or I/O channels from BASIC.) </P> <BR> <BR> <P>Filehandles make it easier for you to get input from and send output <BR> to many different places. Part of what makes Perl a good glue language <BR> is that it can talk to many files and processes at once. Having nice symbolic <BR> names for various external objects is just part of being a good glue language.[17] <BR> </P> <BR> <BR> <BLOCKQUOTE class=footnote> <BR> <P>[17] Some of the other things that make Perl a good glue language are: <BR> it's 8-bit clean, it's embeddable, and you can embed other things in it <BR> via extension modules. It's concise, and networks easily. It's environmentally <BR> conscious, so to speak. You can invoke it in many different ways (as we <BR> saw earlier). But most of all, the language itself is not so rigidly structured <BR> that you can't get it to &quot;flow&quot; around your problem. It comes <BR> back to that TMTOWTDI thing again. </P> <BR> </BLOCKQUOTE> <BR> <BR> <P>You create a filehandle and attach it to a file by using the <A HREF="ch03_02.htm#PERL2-CMD-OPEN">open</A> <BR> function. <A HREF="ch03_02.htm#PERL2-CMD-OPEN">open</A> takes two parameters: <BR> the filehandle and the filename you want to associate it with. Perl also <BR> gives you some predefined (and preopened) filehandles. <TT>STDIN</TT> is <BR> your program's normal input channel, while <TT>STDOUT</TT> is your program's <BR> normal output channel. And <TT>STDERR</TT> is an additional output channel <BR> so that your program can make snide remarks off to the side while it transforms <BR> (or attempts to transform) your input into your output.[18] </P> <BR> <BR> <BLOCKQUOTE class=footnote> <BR> <P>[18] These filehandles are typically attached to your terminal, so you <BR> can type to your program and see its output, but they may also be attached <BR> to files (and such). Perl can give you these predefined handles because <BR> your operating system already provides them, one way or another. Under <BR> UNIX, processes inherit standard input, output, and error from their parent <BR> process, typically a shell. One of the duties of a shell is to set up these <BR> I/O streams so that the child process doesn't need to worry about them. <BR> </P> <BR> </BLOCKQUOTE> <BR> <BR> <P>Since you can use the <A HREF="ch03_02.htm#PERL2-CMD-OPEN">open</A> <BR> function to create filehandles for various purposes (input, output, piping), <BR> you need to be able to specify which behavior you want. As you would do <BR> on the UNIX command line, you simply add characters to the filename. </P> <BR> <BR> <PRE>open(SESAME, &quot;filename&quot;); # read from existing file <BR> open(SESAME, &quot;&lt;filename&quot;); # (same thing, explicitly) <BR> open(SESAME, &quot;&gt;filename&quot;); # create file and write to it <BR> open(SESAME, &quot;&gt;&gt;filename&quot;); # append to existing file <BR> open(SESAME, &quot;| output-pipe-command&quot;); # set up an output filter <BR> open(SESAME, &quot;input-pipe-command |&quot;); # set up an input filter <BR> </PRE> <BR> <BR> <P>As you can see, the name you pick is arbitrary. Once opened, the filehandle <BR> <TT>SESAME</TT> can be used to access the file or pipe until it is explicitly <BR> closed (with, you guessed it, <TT>close(SESAME)</TT>), or the filehandle <BR> is attached to another file by a subsequent <A HREF="ch03_02.htm#PERL2-CMD-OPEN">open</A> <BR> on the same filehandle.[19] </P> <BR> <BR> <BLOCKQUOTE class=footnote> <BR> <P>[19] Opening an already opened filehandle implicitly closes the first <BR> file, making it inaccessible to the filehandle, and opens a different file. <BR> You must be careful that this is what you really want to do. Sometimes <BR> it happens accidentally, like when you say <TT>open($handle,$file)</TT>, <BR> and <TT>$handle</TT> happens to contain the null string. Be sure to set <BR> <TT>$handle</TT> to something unique, or you'll just open a new file on <BR> the null filehandle. </P> <BR> </BLOCKQUOTE> <BR> <BR> <P>Once you've opened a filehandle for input (or if you want to use <TT>STDIN</TT>), <BR> you can read a line using the line reading operator, <TT>&lt;&gt;</TT>. <BR> This is also known as the angle operator, because of its shape. The angle <BR> operator encloses the filehandle (<TT>&lt;SESAME&gt;</TT>) you want to <BR> read lines from.[20] An example using the <TT>STDIN</TT> filehandle to <BR> read an answer supplied by the user would look something like this: </P> <BR> <BR> <BLOCKQUOTE class=footnote> <BR> <P>[20] The empty angle operator, <TT>&lt;&gt;</TT>, will read lines from <BR> all the files specified on the command line, or <TT>STDIN</TT>, if none <BR> were specified. (This is standard behavior for many UNIX filter programs.) <BR> </P> <BR> </BLOCKQUOTE> <BR> <BR> <PRE>print STDOUT &quot;Enter a number: &quot;; # ask for a number <BR> $number = &lt;STDIN&gt;; # input the number <BR> print STDOUT &quot;The number is $number\n&quot;; # print the number <BR> </PRE> <BR> <BR> <P>Did you see what we just slipped by you? What's the <TT>STDOUT</TT> <BR> doing in those <A HREF="ch03_02.htm#PERL2-CMD-PRINT">print</A> statements <BR> there? Well, that's one of the ways you can use an output filehandle. A <BR> filehandle may be supplied as the first argument to the <A HREF="ch03_02.htm#PERL2-CMD-PRINT">print</A> <BR> statement, and if present, tells the output where to go. In this case, <BR> the filehandle is redundant, because the output would have 
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -