?? the perl weekly journal 3rd edition.html
字號:
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<META NAME="Generator" CONTENT="Microsoft Word 97">
<TITLE>Ankit Fadia ankit@bol.net.in</TITLE>
</HEAD>
<BODY LINK="#0000ff">
<FONT SIZE=2><P>_______________________________________________________________</P>
<P>The Perl Almost Weekly Manual By Ankit Fadia </FONT><A HREF="mailto:ankit@bol.net.in"><FONT SIZE=2>ankit@bol.net.in</FONT></A></P>
<FONT SIZE=2><P>_______________________________________________________________</P>
<B><P>@ARGV</P>
</B><P>Till now we have learnt about only one special Variable, the $_ variable. Another useful variable is the @ARGV variable which contains a set of elements of all command line arguments provided to the Perl Program. The first command line argument can be found at $ARGV[0], the second at $ARGV[1] and so on. Let's suppose a Perl Program was called using three command line arguments, which are</P>
<P> Ankit, Fadia, Anki123. Then @ARGV would contain Ankit, Fadia and Anki123, with $ARGV[0]=Ankit, $ARGV[1]= Fadia and $ARGV[2]= Anki123.</P>
<P>For Example,</P>
<P>The following PERL program takes the number which was passed as a command line argument, then multiplies this argument with 4 and finally displays the result on the screen.</P>
<P>$number = $ARGV[0];</P>
<P>print $number *4;</P>
<P>Output:</P>
<P>C:\perl>perl filename.pl 5</P>
<P>20</P>
<P>Not all functions assume $_ to be the default variable, some functions take @ARGV instead. For Example, the shift( ) function. What the shift( ) function does is remove and return the leftmost or the first argument of @ARGV. To understand the use of the shift( ) function, consider the following example.</P>
<P>In this PERL program ,we assume that the User Passes two arguments, the Program then takes the two arguments adds them and then displays the sum on the screen. </P>
<P>$first = shift;</P>
<P>$second = shift;</P>
<P>print $first + $second;</P>
<P>Output:</P>
<P>C:\perl>perl filename.pl 3 5</P>
<P>8</P>
<B><P>Input Output</P>
</B>
<P>Until Now we know of only two kinds of data types-: Scalars and Arrays. Well in this chapter, we will learn about a new third kind of data type called Filehandles. These filehandles act as a bridge between the Perl Program and other files, directories or other programs.</P>
<P>In fact, we have already come across file handles in the earlier chapters, without even noticing it. Remember that we used the angle brackets < >, for two purposes: to read files and to get User Input. Now in both the cases, the angle brackets automatically without you knowing of it, opened a filehandle.</P>
<P>The Standard Perl filehandles are-:</P>
<P> </P>
<P>STDIN		Standard Input		Used to Get User Input</P>
<P>STDOUT Standard Output		The Program output, which is usually displayed on the screen.</P>
<P>STDERR Standard Error		Error Messages to be displayed on the Screen</P>
<P>Just like STDIN is used to get User Input and ARGV is used to read command line data. All this might seem a bit confusing, but believe me, it really is not. Read on to understand better.</P>
<P>Say you give a command like:</P>
<P>$varname= < >;</P>
<P>then PERL automatically fills the angle brackets with either STDIN or ARGV. But how does PERL decide which handle should be inserted? Good Question. You see, whenever PERL comes across angle brackets, it checks to see whether there is still data left to be read from the command line files. If there is data left, then the angle brackets behaves as <ARGV> else they behave as <STDIN>.</P>
<P>We can also manually differentiate between the two by mentioning either one of them within the angle brackets. So basically we can say that the code <FILE> will read data from FILE.</P>
<B><P>Opening Files For Reading</P>
</B>
<P>Well now that you know what file handles do, lets learn how to open a text file to read it's data and print this read data on the screen. The following code does just that-:</P>
<P>open(SITES, "sitelist.txt"); # Open sitelist.txt and name the connection SITES</P>
<P>while (<SITES>) {</P>
<P>print; # Read the next line from SITES and print it on the screen</P>
<P>}</P>
<P>Now let's analyze each line of the code snippet. In the first line, we use the open ( ) function (discussed in detail later) to open the file sitelist.txt and give the connection a friendly name of our choice, in this case we name the connection SITES. We can give it any name of our choice, even something like your own name or simply 'a'.</P>
<P>The second line is the same as </P>
<P>while($_= <SITES>);</P>
<P><BR>
We are simply making better, more efficient scripts. It basically reads the next line of sitelist.txt using the opened connection, SITES. This line is then assigned to the default variable $_ , which is finally printed by the third line. Perl reads one by one each line of the file opened.</P>
<P>Now let's say if the file sitelist.txt does not exist, then normally PERL displays a cryptic difficult to understand error message which is of no use to a novice to deduce what went wrong. How can we display our own customised error messages instead of using the cryptic PERL error messages? Well the die ( ) function holds the key.</P>
<P>The basic syntax of the command is-:</P>
<P> </P>
<P>die(STRING): This code exits the program with the current value of the special variable $! (Discussed Later) and prints STRING on the screen.</P>
<P>Now let's take an example to see how we can use the die( ) function effectively.</P>
<P>Consider the following snippet of code-:</P>
<P>open (FILE123, "first.txt") | | die "File Not Found";</P>
<P>while (<FILE123>) {</P>
<P>print;</P>
<P>}</P>
<P>The above program attempts to open the first.txt text file and if it cannot do so for some reason, it displays the cutomised error message, File Not Found on the screen.</P>
<P>Another Special Variable: $!</P>
<P>Earlier I had mentioned a new kind of special variable, the $! Variable. </P>
<P>The $! Or $ERRORNO or $OS_ERROR Variable contains the system error code. In Numeric context ($!) it contains the error number and in character context ($ERRORNO or $OS_ERROR) it contains the error message.</P>
<P>For Example,</P>
<P>print $!; #Prints current Error Number</P>
<P>print $ERRORNO; #Prints Error Message associated with the current Error Number</P>
<P>The Error Number to Error Message table is as follows:</P>
<P> </P>
<OL>
<LI>Not Owner</LI>
<LI>No Such File or Directory</LI></OL>
<OL START=5>
<LI> Input/Output Error</LI></OL>
<OL START=13>
<LI> Permission Denied</LI></OL>
<OL START=17>
<LI> File Exits</LI></OL>
<OL START=28>
<LI> No Space Left on Drive</LI></OL>
<P>This variable is normally is used in the following context;</P>
<P>Open(FILE123, "xyz.doc") || die "File Not Found: $!"</P>
<P>Will display something like the following error message if the file xyz.doc does not exist:</P>
<P>File Not Found: 2</P>
<P>Sometimes, when an error occurs, instead of exiting entirely from the program we might need to display only a Warning Message advising the user what to do next as an error has occurred. This is where the warn ( ) function comes in. This function too works like the die ( ) function the only difference being that the program is not exited.</P>
<P>For Example,</P>
<P>warn("Caution!! System Resource Low. Please Close some programs before continuing");</P>
<P> </P>
<P>Till now we have learnt only how to read from a file, now let抯 learn how to write or even append to a file. Again we make use of the lovely function open( ). Its basic syntax is as follows-:</P>
<P>open(FILEHANDLE, FILENAME): Opens the file FILENAME and gives the connection the name, FILEHANDLE.</P>
<P>If the open ( ) function is successful it return the TRUE value else it return UNDEF.</P>
<P>Whether a file has been opened for only reading, only writing, appending, or both writing and reading depends on the character at the beginning or preceding FILENAME.</P>
<P>If FILENAME begins with < (or nothing) the file is opened for Reading only.</P>
<P>If FILENAME begins with > then the file is opened for Writing only.</P>
<P>If FILENAME begins with >> then the file is opened for appending.</P>
<P>If FILENAME begins with +(i.e. +>, <+ or +>>) then the file is opened for reading and </P>
<P> 						 writing(appending).</P>
<P>If FILENAME is - then STDIN is opened</P>
<P>If FILENAME is >- then STDOUT is opened</P>
<P>The following are some Practical examples, that will make you understand the various characters better-:</P>
<P>open(FILE, ">>$filename") or die" File not Found"; #File opened for Appending</P>
<P>open(FILE, "+<$filename") or die" File not Found"; #File opened for both reading and writing.</P>
<P>open(FILE, "+>$filename") or die" File not Found"; #File opened for both reading and writing</P>
<P>open(FILE, ">$filename") or die" File not Found"; #File opened for writing.</P>
<P>NOTE: When you try to open a file with the > character then PERL checks to see if the file by the specified filename exists or not. If yes then the File is overwritten else a new file by the specified filename is created.</P>
<P>The close( ) function is used to close a file connection using the open ( ) function. The basic syntax of the close( ) function is as follows:</P>
<P>Close(FILEHANDLE);</P>
<P>Examples:</P>
<P>print "USERNAME:";</P>
<P>chomp ($user= < >);</P>
<P>print "Password:";</P>
<P>chomp($pass= < >);</P>
<P>open(LOGFILE, ">>log.txt") or die " Please Re Login";</P>
<P>close(LOGFILE);</P>
<P>The above code simply logs or appends the Usernames and Passwords typed by the User to a log file, log.txt and then finally closes the File Handle associated with it.</P>
<P>The following PERL program logs all keystrokes of the User until he types EXIT\n.</P>
<P>While(10) {</P>
<P>$input = <>; #Infinite Loop</P>
<P>last if $input eq "exit\n"; #Exit loop when User types Exit followed by Enter.</P>
<P>open(FILE, ">>xyz.txt");</P>
<P>print FILE $input; # Print data typed by User in the file xyz.txt</P>
<P>}</P>
<P>close FILE;</P>
<B><P>MOVING AROUND IN A FILE</P>
</B>
<P>Now that we know how to open a file for various purposes, how can we move around within a file and how can we tell where you are?</P>
<P>You see, whenever a file handle is used, it has an associated file pointer. When we open a file, the file pointer by default points to 0. This file pointer changes automatically accordingly as we move or read data from the file using the filehandle. This process is automatic and occurs without any User intervention.</P>
<P>But sometimes, we manually want to change the position of the file pointer and make it point elsewhere. This is when the seek( ) and tell( ) functions come into the picture.</P>
<B><P>seek( )</P>
</B>
<P>Basic Syntax of the seek command is:</P>
<P>seek(FILEHANDLE, STEPS, FROM); :Moves the file pointer of the FILEHANDLE to number of STEPS from FROM.</P>
<P>Note: File Positions are measured in Bytes.</P>
<P>The value of FROM can be 0 for the beginning of the file, 1 for the current position and 2 for the end of the file.</P>
<P>The following example will really make you comfortable using the seek( ) command:</P>
<P>seek(FILE, 0, 0); # moves to beginning of file</P>
<P>seek(FILE, 157,0); #moves to 157 bytes from the beginning of the file.</P>
<P>seek(FILE,45,1); #moves 45 bytes ahead.</P>
<P>seek(FILE, -45,1); #moves 25 bytes behind.</P>
<P>seek(FILE,200,2); #moves 200 bytes from the End of File.(EOF)</P>
<P>I hope by know you would probably know the seek( ) command like the back of your hand.</P>
<B><P>tell( )</P>
</B>
<P>The tell() command returns the current position of the file pointer.</P>
<P>For Example, </P>
<P> </P>
<P>print tell(FILE);</P>
<P>prints the current position of the file pointer.</P>
<B><P>Truncating Files</P>
</B>
<P>The truncate( ) function is used to truncate or shorten a file to the specified number of bytes. Its basic syntax is:</P>
<P>truncate(FILE, NUM); :Shortens the FILE to NUM number of bytes.</P>
<P>For Example,</P>
<P>truncate("xyz.txt", 100); #Shortens xyz.txt to 100 bytes.</P>
<P>The same above result can also be achieved by the following lines of code:</P>
<P>open(FILE, "+<xyz.txt);</P>
<P>truncate(FILE, 100);</P>
<B><P>Deleting Files</P>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -