Contents Perl File read

PERL - Reading from a File

It is possible to read lines from files and input them using the <> input operator. By placing the file handle inside of the input operator, your script will input that line of the file.

PERL Code:

#!/usr/bin/perl

print "content-type: text/html \n\n"; #The header
$HTML = "myhtml.html";
open (HTML) or die "Can't open the file!";
print <HTML>;
close (HTML);

myhtml.pl:

Here we have an HTML page with a paragraph.

Here we use a tiny PERL script to display several lines of HTML code. Each line is stored into an array and automatically printed to the browser in HTML format using the <> input operator.

PERL - Input Array

PERL is able to print out lines of other files with the use of arrays. Following the example above when we called our HTML file handle using the input operator, PERL automatically stored each iine of the file into a global array. It is then able to process each element of the array as we demonstrated in PERL Arrays. This makes it possible to integrate dynamic bits of HTML code with already existing HTML files.

PERL Code:

#!/usr/bin/perl

print "content-type: text/html \n\n"; #The header
$HTML = "myhtml.html";
open (HTML) or die "Can't open the file!";
@fileinput = <HTML>;
print $fileinput[0];
print $fileinput[1];
print $fileinput[2];
print $fileinput[3];
print "<table border='1' align='center'><tr>
<td>Dynamic</td><td>Table</td></tr>";
print "<tr><td>Temporarily Inserted</td>
<td>Using PERL!</td></tr></table>";
print $fileinput[4];
print $fileinput[5];
close (HTML);

dynamic.pl:

Dynamic Table
Temporarily Inserted Using PERL!

Here we have an HTML page with a paragraph.

To permanently change a file, replace the print function with the printf function.

Want Some more information and Video ???

Home