Getting Set up to work with Perl the First time on a Machine: Install Perl,
DOS Setup
Starting a Perl Sessions - After the first time: DOS directory & path,
Windows Editor
| 1 |
review |
Click here to review Day One !
|
|
| 2 |
printing "special" characters |
print "\nThis has a \\n at the start and end of the line.\n";
print "\n\nIt might be clearer to state: \n";
print " This has a \"\\n\" at the
start and end of the line.\n";
print "\nThe trick is to put a \"\\\" in front of any \"special\"
character to be printed\n";
print "A \"\$\" is also a special character.
# NOTE: so far we have done most printing using " "
and this use of the "\" for
# "special"
characters only applies to things inside of " ",
or inside of ' '.
|
|
| 3 |
opening a file |
open (INFILE, "dataFile.txt");
# assumes that a file named "dataFile.txt"
exists and has some text in it
|
|
| 4 |
Open & read from opened file |
open (INFILE, "dataFile.txt");
$lineRead = <INFILE>;
print "The line read was $lineRead";
|
|
| 5 |
Removing end of line |
open (INFILE, "dataFile.txt");
$lineRead = <INFILE>;
print "The line read was [$lineRead] which still has a new line character
in it.\n ";
chop($lineRead);
print "The line read, [$lineRead] no longer has a new line character
in it.\n";
|
|
| 6 |
Read every line in the file |
open (INFILE2, "dataFile.txt");
$count = 1;
while ($in = <INFILE2>)
{
chop
($in);
print "Line
number $count is [$in].\n"
$count
= $count + 1;
}
|
|
| 7 |
Dealing with file not found |
open (INFILE, "dataFile.txt") or| die "dataFile.txt not found!";
# above line
shows message and exits if file is not found
$lineRead = <INFILE>;
print "The line read was $lineRead"; |
|
| 8 |
Opening a file for output |
open (OUTFILE, ">dataOut.txt"); #opens empty file, erases
old file of same name
# File is opened and permits program to write
new lines of text into file
|
|
| 9 |
write to file |
open (OUTFILE, ">dataOut.txt");
print (OUTFILE "This line of text will be written into
the file.");
print OUTFILE "This will be the second line of text in the output file.";
# after running this program, open the
output file with Notepad. Does it contain
# these two lines?
|
|
| 10 |
Read from
one file, write to second
|
open (OUTF, ">fileOut.txt");
open (INFILE2, "dataFile.txt");
$count = 1;
while ($in = <INFILE2>)
{
print
" read Line number $count, and wrote to fileOut.txt.\n"
print
OUTF $in;
$count
= $count + 1;
}
|
|
| 11 |
Exercise |
Exercise: See if you can write a program to read a number from a file
and then have the user keep guessing the number till they get it right.
Click here for the answer!
|
|
| 12 |
Some file name stuff |
$inPath = "dataFile.txt";
open (INF, $inPath) or die "could not open $inPath";
$outPathPart = "OutFile_";
$count = 1;
while ($in = <INF>)
{
$outPath
= $outPathPart."count"; # the period between the
two text lines appends
#
the first two the second, e.g OutFile_1
open
(OUTF, $outPath) or die "Could not open $outPath!";
print
OUTF $in;
print
" read Line number $count, and wrote to $outPath.\n"
$count
= $count + 1;
close
(OUTF); # close the file when you are through writing to it.
}
|
|
| 13 |
appending lines to the bottom of a file
Exercise |
NOTES:
The Perl statement : open (OUTF, ">outfile.txt"); creates
a new, empty file. If a file of the same name aleady existed, it is deleted.
To open an existing file so that the program can add lines
to the bottom of the file use this Perl statement: open(OUTF,">>outfile.txt);
Note the use of ">>" rather
than just ">"
Write a Perl program that will ask any number of users for their name
and append each name to the bottom of a previously existing output file.
Click here for answer.
|
|
| |
Exercise |
Create three data files. Store the
name of each file on a separate line in a fourth data file. Write a program
to read the filenames from the last file and then open and read each file
identified in the first file opened. Click here
for answer.
|
|
| |
|
|
|
| |
|
|
|
| |
|
|
|
| |
|
|
|
| |
|
|
|
| |
|
|
|
| |
|
|
|
| |
|
|
|