无法取消关联

时间:2013-03-25 13:57:46

标签: linux perl unlink

我对unlink()感到困惑:

my $file = "\"/home/user/Documents/Programming/Perl/extracted/Prueba con formateo HTML/msg-2575-4.html\"";
unlink($file) or warn "Could not unlink $file: $!";

将抛出

Could not unlink "/home/user/Documents/Programming/Perl/extracted/Prueba con formateo HTML/msg-2575-4.html": No such file or directory

虽然文件确实存在:

$ ls -l "/home/user/Documents/Programming/Perl/extracted/Prueba con formateo HTML/msg-2575-4.html"
-rw-rw-r-- 1 user user 413 Mar 25 13:41 /home/user/Documents/Programming/Perl/extracted/Prueba con formateo HTML/msg-2575-4.html

编辑:我也试过了:

my $file = "/home/user/Documents/Programming/Perl/extracted/Prueba con formateo HTML/msg-2575-4.html";
my $file = '/home/user/Documents/Programming/Perl/extracted/Prueba con formateo HTML/msg-2575-4.html';
my $file = "\'/home/user/Documents/Programming/Perl/extracted/Prueba con formateo HTML/msg-2575-4.html\'";

同样的错误。

EDIT2 :choroba要求的更多测试

使用-f测试文件存在会返回false。

这是真实文件名的hexdump:

$ ls "/home/yasin/Documents/Programming/Perl/extracted/Prueba con formateo HTML/msg-2575-4.html" | hexdump -c
0000000   /   h   o   m   e   /   y   a   s   i   n   /   D   o   c   u
0000010   m   e   n   t   s   /   P   r   o   g   r   a   m   m   i   n
0000020   g   /   P   e   r   l   /   e   x   t   r   a   c   t   e   d
0000030   /   P   r   u   e   b   a       c   o   n       f   o   r   m
0000040   a   t   e   o       H   T   M   L   /   m   s   g   -   2   5
0000050   7   5   -   4   .   h   t   m   l  \n                        
000005a

2 个答案:

答案 0 :(得分:4)

文件名不包含双引号。不要将它们包含在变量的值中。

my $file = '/home/user/Documents/Programming/Perl/extracted/Prueba con formateo HTML/msg-2575-4.html';
unlink $file or warn "Could not unlink $file: $!";

答案 1 :(得分:1)

my $file = "\"/home/.../msg-2575-4.html\"";
unlink($file)

相当于做

rm "\"/home/.../msg-2575-4.html\""

显然,正确的shell命令是

rm "/home/.../msg-2575-4.html"

所以你想要

my $file = "/home/.../msg-2575-4.html";
unlink($file)

my $file = '/home/.../msg-2575-4.html';
unlink($file)

如果第二个rm命令有效,那么Perl命令也是如此。