ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
13. File and Directory ManipulationContents: This chapter shows you how to manipulate the files themselves, not merely the data contained in them. We'll use the UNIX (and POSIX and Linux) semantics for demonstrating access to files and directories. Not all filesystems access mechanisms, but these are the standard ones for reasonably support-rich filesystem models. 13.1 Removing a FileEarlier, you learned how to create a file from within Perl by opening it for output with a filehandle. Now, we'll get dangerous and learn how to remove a file (very appropriate for Chapter 13, File and Directory Manipulation, don't you think?). The Perl unlink ("fred"); # say goodbye to fred print "what file do you want to delete? "; chomp($name = <STDIN>); unlink ($name); The unlink ("cowbird","starling"); # kill two birds unlink <*.o>; # just like "rm *.o" in the shell The glob is evaluated in a list context, creating a list of filenames that match the pattern. This is exactly what we need to feed The return value of foreach $file (<*.o>) { # step through a list of .o files unlink($file) || warn "having trouble deleting $file: $!"; } If the If the foreach (<*.o>) { # step through a list of .o files unlink || warn "having trouble deleting $_: $!"; } |