read_file'File :: Slurp' - sysopen:没有这样的文件或目录

Tud*_*tin 1 perl readfile fileslurp

这可能有什么问题?

我试图显示现有文件的内容:

perl -MFile::Slurp -e 'print File::Slurp->read_file("/tmp/001.jpg", { binmode => ":raw" } ) if -e "/tmp/001.jpg"; '
Run Code Online (Sandbox Code Playgroud)

我收到错误:

read_file 'File::Slurp' - sysopen: No such file or directory
Run Code Online (Sandbox Code Playgroud)

该文件存在,print仅执行获取if -e "/tmp/001.jpg"

Mor*_*kus 8

通过File::Slurp::read_file而不是调用函数File::Slurp->read_file.在后一种情况下,Perl的对象系统发挥作用,传递给的第一个参数read_file将是之前的事物->- 意味着它的第一个参数将是字符串"File::Slurp"而不是您实际想要读取的文件名.

这也是您调用new要从中创建新实例的包的原因.

  • 实际上,`-MFile :: Slurp`将导出`read_file`函数,所以你根本不需要`File :: Slurp ::`部分.(但不会有伤害.) (3认同)