0

我对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测试文件是否存在。

这是真实文件名的十六进制转储:

$ 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
4

2 回答 2

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: $!";
于 2013-03-25T14:00:12.103 回答
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 命令也有效。

于 2013-03-25T20:16:45.903 回答