Q&A

Q1:My open statement keeps failing, and I'm not sure why. What's wrong?
A1: 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.
Q2:I'm writing to the file, but nothing seems to go into it. Where's my output going?
A2: 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:
open(FH, ">c:	emp
otes.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.

Q3:When I try to open a file, the open fails and Perl reports permission denied. Why?
A3: 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.
Q4:How can I read a single character at a time?
A4: 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.
Q5:How do I keep other programs from writing to the same file at the same time?
A5: 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.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.19.244.187