YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

Open and read data files in Perl Options
Alba
#1 Posted : Tuesday, February 23, 2010 4:40:07 PM
Rank: Newbie


Groups: Registered

Joined: 2/23/2010
Posts: 1
Location: Las Vegas
I am new to Perl, I have been asked to work with an existing system that is written in Perl. I could use some help opening and reading data files from within Perl.
Geek
#2 Posted : Tuesday, February 23, 2010 4:45:13 PM
Rank: Newbie


Groups: Registered

Joined: 7/10/2009
Posts: 4
Location: Southern California
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);


Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

FlatEarth Theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.3 | YAF © 2003-2009, Yet Another Forum.NET
This page was generated in 0.098 seconds.