1

What could be wrong here?

I am trying to display the content of an existing file:

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

and I get the error:

read_file 'File::Slurp' - sysopen: No such file or directory

The file exists, the print gets executed only if -e "/tmp/001.jpg"

Tudor Constantin
  • 26,330
  • 7
  • 49
  • 72

1 Answers1

8

Call the function via File::Slurp::read_file instead of File::Slurp->read_file. In the latter case Perl's object system comes into play, and the first argument passed to read_file will be the thing before the -> -- meaning its first argument will be the string "File::Slurp" instead of the file name you actually want to read.

That's also why you call new on the packages you want to create a new instance from.

Borodin
  • 126,100
  • 9
  • 70
  • 144
Moritz Bunkus
  • 11,592
  • 3
  • 37
  • 49
  • 3
    Actually, `-MFile::Slurp` will export the `read_file` function, so you don't need the `File::Slurp::` part at all. (It won't hurt, though.) – cjm Aug 25 '12 at 14:38