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
本文详细介绍了Perl语言中的unlink函数用于删除文件的用法及注意事项,并对比了内置mkdir与mkpath、rmdir与rmtree的功能与用途。通过实例展示了如何安全高效地管理文件和目录。

被折叠的 条评论
为什么被折叠?



