Home > Articles > Web Development > Perl

  • Print
  • + Share This
This chapter is from the book


  1. My open statement keeps failing, and I'm not sure why. What's wrong?

  1. First, check the syntax of the open statement. Make sure you're opening the right filename. Print the name before the open if you need to be sure. If you intend to write to the file, make sure you put a > in front of the filename; you need to. Most importantly, did you check the exit status of open by using open() || die "$!"; syntax? The die message might be very important in helping you find your mistake.

  1. I'm writing to the file, but nothing seems to go into it. Where's my output going?

  1. Are you sure that the filehandle opened properly? If you used the wrong filename, your data could be going to the wrong file. A common mistake is to open a file for writing by using backslashes in the pathname and enclosing the pathname in double quotation marks, as shown here:

  2. open(FH, ">c:\temp\notes.txt") || die "$!"; #WRONG!

    This line creates a file called c:(tab)emp(newline)otes.txt—probably not what you had in mind. Also, make sure that your open function succeeded. If you write to a filehandle that hasn't been properly opened, Perl discards the output silently unless you have Perl's warnings enabled.

  1. When I try to open a file, the open fails and Perl reports permission denied. Why?

  1. Perl follows your operating system's rules about file security. If you don't have permission to access the file, the directory, or the drive that the file resides on, neither will your Perl program.

  1. How can I read a single character at a time?

  1. From a file, the Perl function getc can do single-character input. Reading a single character from the keyboard is much more difficult, because character-at-a-time input is highly dependent on your operating system. After you've learned about modules in Hour 15, "Finding Permanence," and have read up on the Perl FAQ in Hour 16, "The Perl Community," check section 5 of the Perl FAQ. The FAQ contains a very lengthy explanation of how to read single characters for various platforms, with lots of code examples. Most of the code there is beyond the scope of this book.

  1. How do I keep other programs from writing to the same file at the same time?

  1. What you're interested in is called file locking. File locking is covered in Hour 15. Be forewarned; it's not particularly easy or foolproof.

  • + Share This
  • 🔖 Save To Your Account