📄 the perl weekly journal 3rd edition.html
字号:
</B><P>To delete files, we use the unlink( ) function whose basic syntax is as follows:</P>
<B>
</B><P>unlink(FILENAMES); :Deletes File Names </P>
<P>This function returns the number of files successfully deleted.</P>
<P>For Example,</P>
<P>unlink "log.txt"; #Deletes log.txt</P>
<P>This PERL function can be very useful to delete temporary files from the hard disk and save space. Consider the following PERL program which deletes all .bak, *.~ and *.tmp files are the disk.</P>
<P>$name = shift;</P>
<P>@old = ($name. '.bak', $name. '.~', $name. '.tmp');</P>
<P>foreach(@old) {</P>
<P>Print "Deleting $_";</P>
<P>}</P>
<P>unlink @old;</P>
<B><P>FILE TESTS</P>
</B><P>Files come in all sizes, properties and types. PERL has certain file tests which inspect File characteristics and properties.</P>
<P>File tests are very funny looking with a single hyphen followed by a special character.</P>
<P>The basic format of file tests is as follows-:</P>
<P>-TEST FILE: is true only if FILE satisfies TEST. FILE can be either a filename of a filehandle.</P>
<P>The following is a complete list of file tests and their meanings-:</P>
<P>FILE TEST		Meaning</P>
<P>-e			File Exists</P>
<P>-f			File is a plain text</P>
<P>-d			File is a directory</P>
<P>-T			File is a text file</P>
<P>-B			File is a Binary File</P>
<P>-r			File is readable</P>
<P>-w			File is writable</P>
<P>-x			File is executable</P>
<P>-s			Size of File(Returns Number of Bytes)</P>
<P>-z			File is Empty i.e. is of Zero Bytes</P>
<P> </P>
<P>Now that you have the complete list of files tests, lets see how to practically make use of them. The following is a collection of examples which will make file tests easier to understand.</P>
<P>Examples,</P>
<P>if (-d "/etc") {</P>
<P>print "Directory Exists"; #Prints message if /etc is a directory</P>
<P>}</P>
<P>The following is an example of a PERL program which checks if a file exists.</P>
<P>$file = shift;</P>
<P>if(-e $file){</P>
<P>print "$file Exists. \n";</P>
<P>}</P>
<P>Every file is either a -T or -B. Perl looks at the beginning of the file. If there are a lot of strange characters, then it is a Binary File else it is a Text File. But empty files satisfy both the -T and -B condition. The following Perl Program checks to see if a file is empty or not.</P>
<P>$file = shift;</P>
<P>if(-z $file){</P>
<P>print "$file Exists. \n";</P>
<P>}</P>
<P> </P>
<P>The stat( ) function</P>
<P>The file tests that we have learnt till now check only a single specific property of a file, however on the other hand, the stat( ) function returns an array of 13 file statistics.</P>
<P>The syntax of the stat( ) command is-:</P>
<P>stat(FILE) : It returns a 13 element array containing the vital or important statistics of the FILE. The FILE can either be a FILE or a FILEHANDLE.</P>
<P>The elements of the stat array are-:</P>
<P>Index		Value</P>
<OL START=0>
<LI>The Device</LI>
<LI>The File's inode</LI>
<LI>The File's mode</LI>
<LI>The Number of Hard Links to the File.</LI>
<LI>The user ID of the file's owner</LI>
<LI>The Group ID of the file</LI>
<LI>The raw device</LI>
<LI>The size of the file</LI>
<LI>The last time the file was accessed</LI>
<LI>The last time the file was modified</LI>
<LI>The last time the status of the file was changed</LI>
<LI>The block size of the system</LI>
<LI>The Number of blocks used by the file</LI></OL>
<P>Let's take an example in which we use the stat( ) array to compute the size of a file.</P>
<P>$file = shift;</P>
<P>$size = (stat($file))[7]; #Returns the eight element of the stat( ) array.</P>
<P>print "Size is: $size";</P>
<P> </P>
<P>The same above result can be obtained by using the see( ) and tell( ) functions.</P>
<P>$file = shift;</P>
<P>open(FILE, "$file");</P>
<P>seek(FILE,0,2);</P>
<P>$size = tell(FILE);</P>
<P>print "Size is: $size";</P>
<P>There is yet another method of finding out the size of a file, using the file tests.</P>
<P>$file = shift;</P>
<P>$size = (-s $filename);</P>
<P>print "Size is: $size";</P>
<B><P>Reading Bytes and not Lines</P>
</B><P>Normally when a Perl program opens a file for reading, it reads a line one by one. However sometimes we need to read bytes and not lines. This is when the read( ) function comes into picture.</P>
<P>The basic syntax of the read( ) command is:</P>
<P>read(FILEHANDLE, VARNAME, BYTES, OFFSET) : Reads BYTES number of bytes starting from OFFSET in FILEHANDLE, placing the result in VARNAME. If OFFSET is not specified, then PERL starts reading from the beginning of the file.</P>
<P>For Example,</P>
<P>open(FILE, "xyz.log") or die "File not found";</P>
<P>read(FILE, $text, 1000);</P>
<P>print $text;</P>
<P>The above snippet of code reads the first 1000 bytes of the file xyz.log and places them into the variable $text and finally prints it on the screen.</P>
<P>The read function can sometimes be less accurate. By that what I mean to say is that sometimes, it reads more bytes than you want it to, due to differences in the Buffer of the system. If this is unacceptable, you should instead use the sysread( ) function, whose basic syntax is as follows:</P>
<P>sysread(FILEHANDLE, VARNAME, BYTES, OFFSET) : Reads BYTES number of bytes starting from OFFSET in FILEHANDLE, placing the result in VARNAME.</P>
<P>This function is almost the same as the read() function accept the fact that it is more accurate and hence sometimes preferable.</P>
<P>Similarly sometimes you may need to write a certain number of bytes from a scalar to a file. For that PERL has the syswrite( ) function, whose syntax is:</P>
<P>syswrite(FILEHANDLE, VAR, BYTES, OFFSET): writes BYTES number of bytes of data from VAR starting at OFFSET to FILEHANDLE.</P>
<P>Both sysread( ) and syswrite( ) return the number of bytes actually read or UNDEF if an error occurs.</P>
<B><P>The getc( ) function</P>
</B>
<P>This functions does what its name suggests, it gets the next character from the filehandle. Its syntax is:</P>
<P>getc(FILEHANDLE): returns the next character from FILEHANDLE.</P>
<P>When the End of File or EOF is reached the value of getc( ) is empty.</P>
<B><P>ACCESSING DIRECTORIES</P>
</B><P>Just as for files you have the open( ) and close( ) functions, for directories, you have the opendir( ) and closedir( ) functions. Their syntax is as follows:</P>
<P>opendir(DIRHANDLE, DIRNAME):opens the directory DIRNAME.</P>
<P>closedir(DIRHANDLE): closes DIRHANDLE</P>
<P>For Example,</P>
<P>opendir(DIR, "/etc"); #opens the /etc directory</P>
<B><P>The readdir Function</P>
</B><P>This is the most used function associated with directories and it's syntax is:</P>
<P>readdir(DIRHANDLE): returns the next file from DIRHANDLE (in scalar context) or the rest of the files (In ARRAY context). If there are no more files, then it return undef.</P>
<P>The following Perl Program uses the opendir( ), closedir( ) and readdir( ) functions to list all files in a particular directory.</P>
<P>$name = shift;</P>
<P>opendir(DIR, $name) or die "Directory invalid: $! \n";</P>
<P>@listoffiles = readdir(DIR);</P>
<P>closedir(DIR);</P>
<P>foreach $file (@listoffiles) {</P>
<P>print "$file\n"; </P>
<P>}</P>
<P>Output: </P>
<P>C:\perl> filename.pl /etc</P>
<P>Ankit.log</P>
<P>Ankit.txt</P>
<P>Passwd.txt</P>
<B><P>Other Directory Functions</P>
</B><P>The following is a list of syntax and use of some other popular Directory Functions:</P>
<B>
</B><P>telldir(DIRHANDLE): returns the position of DIRHANDLE.</P>
<P>seekdir(SIRHANDLE, POSITION): sets DIEHANDLE to read from POSITION which should be </P>
<P>something like the value returned by telldir( )</P>
<P>rewinddir(DIRHANDLE): sets DIRHANDLE to the top of the directory.</P>
<P>mkdir(DIRNAME, MODE) : Creates Directory with the name DIRNAME and mode specified by MODE(Unix).</P>
<P>rmdir(DIRNAME, MODE): Deletes the directory DIRNAME.</P>
<P>chdir(DIR): changes the working directory to DIR.</P>
<P>chroot(DIR): Changes the root directory for the current process to DIR. Only root is allowed to do this.</P>
<P> </P>
<P>Well this is the end of the Perl Section. These Perl Manuals were not aimed at making you a Perl guru but to teach you Perl to that extend to which you could write Useful Programs. For further free Perl Manuals join my mailing list by sending an email to: </FONT><A HREF="mailto:programmingforhackers-subscribe@egroups.com"><FONT SIZE=2>programmingforhackers-subscribe@egroups.com</FONT></A><FONT SIZE=2> There I will distribute manuals on both Perl and C which will make you proper experts.</P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P></FONT></BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -