perl中的unlink:
* unlink
Deletes a list of files. On success, it returns the number of files it successfully deleted. On failure, it returns false and sets $! (errno):
1. my $unlinked = unlink 'a', 'b', 'c';
2. unlink @goners;
3. unlink glob "*.bak";
On error, unlink will not tell you which files it could not remove. If you want to know which files you could not remove, try them one at a time:
1. foreach my $file ( @goners ) {
2. unlink $file or warn "Could not unlink $file: $!";
3. }
Note: unlink will not attempt to delete directories unless you are superuser and the -U flag is supplied to Perl. Even if these conditions are met, be warned that unlinking a directory can inflict damage on your filesystem. Finally, using unlink on directories is not supported on many operating systems. Use rmdir instead.
If LIST is omitted, unlink uses $_ .
2:mkpath and rmtree:
File::Path
有2个方法:mkpath和rmtree,分别为创建目录和删除目录。
perl语言本身自带了 mkdir和rmdir,那为什么还要选用mkpath和rmtree呢?自带的mkdir只能一次创建一级目录,而mkpath则可以一次创建多级;rmdir必须要求目录为空,而rmtree则任意。总之,File::Path为我们提供了另一种创建和删除目录的机制,由用户自己选用。
说明:
a) mkpath(directory, 1, 0711);
第一个参数为目录名,第二个参数为一个bool值,为真的时候打印每次创建的目录,默认为假,最后一个参数为目录的权限。
b) rmtree(directory, 1, 1);
第一个参数为目录名,第二个参数为一个bool值,为真的时候打印删除文件时的一些信息,默认为假,最后一个参数也是一个bool值,为真的时候对没有删除权限的文件直接跳过。
http://perldoc.perl.org/File/Path.html
ulink,mkpath,rmtree
最新推荐文章于 2025-04-01 15:01:00 发布