Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
How to open and read data files with Perl
How to open and read data files with Perl
✍: Guest
Data files are opened in Perl using the open() function. When you open a data file, all you have to do is specify (a) a file handle and (b) the name of the file you want to read from.
As an example, suppose you need to read some data from a file named "checkbook.txt". Here's a simple open statement that opens the checkbook file for read access:
open (CHECKBOOK, "checkbook.txt");
In this example, the name "CHECKBOOK" is the file handle that you'll use later when reading from the checkbook.txt data file. Any time you want to read data from the checkbook file, just use the file handle named "CHECKBOOK".
Now that we've opened the checkbook file, we'd like to be able to read what's in it. Here's how to read one line of data from the checkbook file:
$record = < CHECKBOOK > ;
After this statement is executed, the variable $record contains the contents of the first line of the checkbook file. The "<>" symbol is called the line reading operator.
To print every record of information from the checkbook file
open (CHECKBOOK, "checkbook.txt") || die "couldn't open the file!";
while ($record = < CHECKBOOK >) {
print $record;
}
close(CHECKBOOK);
2013-08-22, 1914👍, 0💬
Popular Posts:
What is the difference between mysql_fetch_object() and mysql_fetch_array() functions in PHP? mysql_...
How will you freeze the requirement in this case? What will be your requirement satisfaction criteri...
What are the two kinds of comments in JSP and what's the difference between them? <%-- JSP Co...
What is normalization? What are different types of normalization? It is set of rules that have been ...
Explain in detail the fundamental of connection pooling? When a connection is opened first time a co...