Q&A

Q1: I'm trying to open a file for writing, but die keeps getting triggered and I can't figure out why. The directory is writable, the file doesn't exist—there's no reason why things should be going wrong.
A1: Did you remember to put the > character at the start of your filename? You need that character to tell Perl that the file is to be written to—otherwise it'll assume the file is to be read from, and if it can't find the file it won't be able to open it.

The open function should work most of the time; it should only fail under unusual circumstances. If it's consistently failing for you, consider double-checking your use of open.

Q2:I want to use a subroutine to open my file, and then pass the file handle around to other subroutines. But ...

Get Sams Teach Yourself Perl in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.