Connecting Tech Pros Worldwide Forums | Help | Site Map

New to Perl and File IO

Newbie
 
Join Date: Sep 2007
Location: Hai Phong city, Vietnam
Posts: 2
#1: Sep 5 '07
Hi everybody,
I am new to Perl (in Vietnam, Perl is a new program to learn)
I've got some first parts of Perl
I am having some problems with I/O handle
How can I create a new text file, save it, open and append it
I don't understand the words in the book
Please tell me know how to take it
Thanks

numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,572
#2: Sep 5 '07

re: New to Perl and File IO


Welcome to TSDN and Welcome to Perl!

First, I would suggest you read the online book Beginning Perl. This will definitely help you get started.

Second, there are many references on the web that you will want to visit quite frequently. CPAN is where you will find all of the Perl modules for use in your coding. Perldoc is where you will find loads of documentation on all things Perl.

Speaking of perldoc, a couple of pages you should examine regarding opening and adding to files are the open() function, which is what is used in perl to open files for reading, writing, reading/writing, appending, etc. And, the perlfunc page, which goes over the standard functions that are included in the default distribution of Perl.

When you open a file, for writing to for instance, you would use the following syntax:

Expand|Select|Wrap|Line Numbers
  1. open(FILEHANDLE, ">./file.txt");
  2.  
This will first check and see if file.txt exists in the current directory. If it does not, it creates it, then, it opens it for writing to. The ">" tells perl it is only to write to the file. FILEHANDLE is the file handle you would use when referencing the open file. Here is how you would print a line of text to the open file:

Expand|Select|Wrap|Line Numbers
  1. print FILEHANDLE ("Print some text to the file.\n");
  2.  
That will output to the opened file referenced by FILEHANDLE. This is just an example to get you started. Read the open() function page I gave you a link to and it will explain more for you. Plus, the online book link I provided will also go over that, plus a whole lot more.

If you have any further questions, please do not hesitate to ask them here. If you are asking about code you are working on, please be sure to provide that code here so we can see it and help you. When supplying code in your postings though, please put code tags around the code as well.

Regards,

Jeff
Reply