Difference between revisions of "File I/O in Perl"

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search
(New page: Use a filehandle to open a file in Perl. The filehandle identifier doesn't have a prefix like other Perl identifiers. To open a file use a FILEHANDLE (a name you decide, all caps isn't a...)
 
m (Reverted edits by Atekysepiko (Talk); changed back to last version by Admin)
 
(8 intermediate revisions by 2 users not shown)
Line 5: Line 5:
 
  open(BOBSFILE, "information.txt");
 
  open(BOBSFILE, "information.txt");
  
The above file will be opened for "read" since no i/o type was specified.  There are three ways to open a file. This is specified with a symbol before the filename.  If no symbol is specified, "read" is assumed.
+
The above file will be opened for "read" since no i/o type was specified.  There are three ways to open a file. This is specified with a symbol before the filename.  If no symbol is specified, "read" is assumed.<big>
# read <            (open an existing file for read)   ex: open BOBSFILE, "<information.txt";
+
# '''read''' <            (''open an existing file for read'')   ex: open BOBSFILE, "<information.txt";
# write >            (create a new file to write)   ex: open NEWSTORY, ">story.txt";
+
# '''write''' >            (''create a new file to write'')   ex: open NEWSTORY, ">story.txt";
# append >>            (add more to an existing file)   ex: open(LOG, ">>activity.log");
+
# '''append''' >>            (''add more to an existing file'')   ex: open(LOG, ">>activity.log");</big>
 +
 
 +
open BOBSFILE, "<information.txt";
 +
open NEWSTORY, ">story.txt";
 +
open(LOG, ">>activity.log");
 +
 
 +
If you open another file using the same FILEHANDLE, Perl will automatically close the previous file and let you use the FILEHANDLE for the new file.
 +
 
 +
To manually close a FILEHANDLE:
 +
 
 +
close FILEHANDLE;
 +
 
 +
Be prepared to deal with uncooperative files.
 +
 
 +
open(FILEHANDLE, ">bobsinfo.dat") or die("Error");
 +
 
 +
Web development example.
 +
 
 +
open (FILE,"$guestbk") || die "Can't Open $guestbk: $!\n";
 +
 
 +
&nbsp;
 +
 
 +
== Open a file to READ ==
 +
 
 +
To open a file and read only the first line:
 +
 
 +
$strVariable = <BOBSFILE>;
 +
# remove CR LF
 +
$strVariable = chomp($strVariable = ); 
 +
 
 +
Read and print all lines as well as number them.
 +
 
 +
open (FILE,"$guestbk") || die "Can't Open $guestbk: $!\n";
 +
$lnum = 1;
 +
while( $line = <FILE> ){
 +
  chomp($line);
 +
  print "$lnum: $line\n";
 +
  $lnum++;
 +
}
 +
close FILE;
 +
 
 +
Read all lines into an array.
 +
 
 +
@eachline = <FILE>;
 +
chomp(@eachline);
 +
print "@eachline";
 +
 
 +
&nbsp;
 +
 
 +
== Open a file to WRITE ==
 +
 
 +
Writing to a file overwrites what was in the file previously and writes the new information over it.
 +
 
 +
<nowiki>open (GUEST,">$guestbk") || die "Can't Open $guestbookreal: $!\n";</nowiki>
 +
<nowiki>print GUEST "<H1>New Guestbook Entry</H1>\n";</nowiki>
 +
<nowiki>print GUEST "$FORM{'realname'}</a></b>";</nowiki>
 +
<nowiki>print GUEST "<b>Time:&nbsp;</b>$date<br>\n";</nowiki>
 +
<nowiki>print GUEST "<b>Comments:&nbsp;</b>$txtComments<br>\n";</nowiki>
 +
close (GUEST);
 +
 
 +
Read from one file and write its contents into another file.
 +
 
 +
my $infile = 'bobsbills.txt';
 +
my $outfile = 'bobsexpenses.txt';
 +
 +
open IN, "< $infile" or die "Can't open $infile : $!";
 +
open OUT, "> $outfile" or die "Can't open $outfile : $!";
 +
print OUT <IN>;
 +
close IN;
 +
close OUT;
 +
 
 +
== Other Methods of File Access ==
 +
 
 +
Here's a quick way to write to a file without using a filehandle.
 +
 +
print `echo "Write this line" > filename.txt"`;
 +
 
 +
Read without filehandle
 +
 
 +
$strLines = `cat filename.txt`;
  
 
&nbsp;
 
&nbsp;

Latest revision as of 12:49, 24 November 2010

Use a filehandle to open a file in Perl. The filehandle identifier doesn't have a prefix like other Perl identifiers.

To open a file use a FILEHANDLE (a name you decide, all caps isn't a requirement but suggested for clarity) and a FILENAME

open(BOBSFILE, "information.txt");

The above file will be opened for "read" since no i/o type was specified. There are three ways to open a file. This is specified with a symbol before the filename. If no symbol is specified, "read" is assumed.

  1. read <            (open an existing file for read)   ex: open BOBSFILE, "<information.txt";
  2. write >            (create a new file to write)   ex: open NEWSTORY, ">story.txt";
  3. append >>            (add more to an existing file)   ex: open(LOG, ">>activity.log");
open BOBSFILE, "<information.txt";
open NEWSTORY, ">story.txt";
open(LOG, ">>activity.log");

If you open another file using the same FILEHANDLE, Perl will automatically close the previous file and let you use the FILEHANDLE for the new file.

To manually close a FILEHANDLE:

close FILEHANDLE;

Be prepared to deal with uncooperative files.

open(FILEHANDLE, ">bobsinfo.dat") or die("Error");

Web development example.

open (FILE,"$guestbk") || die "Can't Open $guestbk: $!\n";

 

Open a file to READ

To open a file and read only the first line:

$strVariable = <BOBSFILE>;
# remove CR LF
$strVariable = chomp($strVariable = );  

Read and print all lines as well as number them.

open (FILE,"$guestbk") || die "Can't Open $guestbk: $!\n";
$lnum = 1;
while( $line = <FILE> ){
  chomp($line);
  print "$lnum: $line\n";
  $lnum++;
}
close FILE;

Read all lines into an array.

@eachline = <FILE>;
chomp(@eachline);
print "@eachline";

 

Open a file to WRITE

Writing to a file overwrites what was in the file previously and writes the new information over it.

open (GUEST,">$guestbk") || die "Can't Open $guestbookreal: $!\n";
print GUEST "<H1>New Guestbook Entry</H1>\n";
print GUEST "$FORM{'realname'}</a></b>";
print GUEST "<b>Time: </b>$date<br>\n";
print GUEST "<b>Comments: </b>$txtComments<br>\n";
close (GUEST);

Read from one file and write its contents into another file.

my $infile = 'bobsbills.txt';
my $outfile = 'bobsexpenses.txt';

open IN, "< $infile" or die "Can't open $infile : $!";
open OUT, "> $outfile" or die "Can't open $outfile : $!";
print OUT <IN>;
close IN;
close OUT;

Other Methods of File Access

Here's a quick way to write to a file without using a filehandle.

print `echo "Write this line" > filename.txt"`;

Read without filehandle

$strLines = `cat filename.txt`;