spliting the html files using perl - perl

I am new to Perl but I have tried to write a program to split a single HTML file into multiple HTML files.
#!/usr/bin/env perl
use strict;
#use warnings;
my #file_names;
## Read the list of file names
open( my $fh, "$ARGV[0]" );
while ( <$fh> ) {
chomp; #remove new line character from the end of the line
push #file_names, $_;
}
my $counter = 0;
my ( $file_name, $fn );
## Read the input file
open( $fh, "$ARGV[1]" );
while ( <$fh> ) {
## If this is an opening class, open the next output file,
## and set $counter to 1.
if ( / class="bch_ha"/ ) {
$counter = 1;
$file_name = shift(#file_names);
open( $fn, ">", "$file_name" );
#print "<html>\n<body>";
}
## If this is a closing class, print the line and set $counter back to 0
if ( /\n<p sourcepage="(\d+)" class="bch_ha"/ ) {
$counter = 0;
print $fn $_;
close($fn);
}
if ( / class="bcesu_tt"/ ) {
$counter = 1;
$file_name = shift(#file_names);
open( $fn, ">", "$file_name" );
#print "<html>\n<body>";
}
if ( /\n<p sourcepage="(\d+)" class="bcekt_tt"/ ) {
$counter = 0;
print $fn $_;
close($fn);
}
if (/ class="bcekt_tt"/ ) {
$counter = 1;
$file_name = shift(#file_names);
open( $fn, ">", "$file_name" );
#print "<html>\n<body>";
}
if ( /\n<p sourcepage="(\d+)" class="bcepq_tt"/ ) {
$counter = 0;
print $fn $_;
close($fn);
}
if ( / class="bcepq_tt"/ ) {
$counter = 1;
$file_name = shift(#file_names);
open( $fn, ">", "$file_name" );
#print "<html>\n<body>";
}
if ( /\n<p sourcepage="(\d+)" class="bcecs_tt"/ ) {
$counter = 0;
print $fn $_;
close($fn);
}
if ( / class="bcecs_tt"/ ) {
$counter = 1;
$file_name = shift(#file_names);
open( $fn, ">", "$file_name" );
#print "<html>\n<body>";
}
if ( /\n<p sourcepage="(\d+)" class="bceex_tt"/ ) {
$counter = 0;
print $fn $_;
close($fn);
}
if ( / class="bceex_tt"/ ) {
$counter = 1;
$file_name = shift(#file_names);
open( $fn, ">", "$file_name" );
#print "<html>\n<body>";
}
if ( /\n<\/body>\n<\/html>/ ) {
$counter = 0;
print $fn $_;
close($fn);
}
## Print into the corresponding file handle if $counter is 1
print $fn $_ if $counter == 1
}
I need to add some more options. The code should ask for manual input for the delimiters and the split files should go to folder name chapterxx. Please help me on this
Yeah Please find the below HTML sample.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8" />
</head>
<body>
<p sourcepage="27" `class="bch_ha"`></p>
<p sourcepage="26" class="bopob_ct">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</p>
<p sourcepage="26" class="bopob_cr">Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx% <i>Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</i></p>
<p sourcepage="26" class="bch_nmword">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</b></p>
<p sourcepage="26" class="bch_nm">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</b></p>
<p sourcepage="26" class="bch_tt">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</p>
<p sourcepage="26" class="bopob_tt">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx% <b>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX% </b>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</p>
<p sourcepage="26" class="bopob_tx">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</i></p>
<p sourcepage="26" class="bopob_lbfirst">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</b></p>
<p sourcepage="26" class="bopob_lb">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</b></p>
<p sourcepage="26" class="bopob_lb">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</b></p>
<p sourcepage="26" class="bopob_lb">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</b></p>
<p sourcepage="26" class="bch_ha">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</b></p>
<p sourcepage="26" class="bopob_lblast">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</b> </p>
<p sourcepage="26" class="bopcs_txfirst">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</p>
<p sourcepage="26" class="bopcs_tx">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</p>
<p sourcepage="26" class="bopcs_tx">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</p>
<p sourcepage="27" class="bopcs_tx">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</p>
<p sourcepage="27" class="bopcs_tx">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%<span class="sup">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</sup>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</p>
<p sourcepage="27" class="bch_txfirst">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</p>
</body>
</html>
I just need to split the html based on the class class="bch_ha" to next class="bch_ha" and write the content in new html named reader_0.html. the file name will be incremental like reader_1.html.

Maybe this example will give you an idea on how to be able to complete your program.
In this example the focus is on how to split files based on the delimiter.
Note: Only the html body is saved.
#!/usr/bin/env perl
# test.pl
use strict;
use warnings;
my $file = './htmlInput.html'; # input file
my $delim = 'class="bch_ha"'; # delimiter
my $dir = 'chapter' . time; # folder with unix timestamp
# mkdir returns 1 if success
if ( mkdir($dir, 0755) ) {
print "INFO: Created folder $dir to collect files.\n";
} else {
die "Can't make folder $dir\n";
}
# reader_x.html, x = [0..]
my $reader = 'reader_0.html';
my $fh2;
my $cnt = 0;
my $delim_first_time = 1;
open(my $fh, "<", $file) or die "Can't open and read $file: $!"; # read file
while (my $li = <$fh>) {
last if ( $li =~ /<\/body>/ ); # quit the while loop
if ( $delim_first_time && $li =~ /$delim/ ) {
open($fh2, ">", "./$dir/$reader") or die "Can't write to $reader : $!"; # write
$delim_first_time = 0;
} elsif ( $li =~ /$delim/ ) {
close($fh2);
$cnt++;
$reader =~ s/[0-9]+/$cnt/; # reader_0.html -> reader_1.html
open($fh2, ">", "./$dir/$reader") or die "Can't write to $reader : $!"; # write
}
print $fh2 $li if !$delim_first_time;
}
close($fh);
close($fh2);
# output:
# [~]$ ./test.pl
# INFO: Created folder chapter1465642603 to collect files.
# [~]$ ls chapter1465642603
# reader_0.html reader_1.html
# [~]$ cat chapter1465642603/reader_0.html
# <p sourcepage="27" `class="bch_ha"`></p>
# <p sourcepage="26" class="bopob_ct">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</p>
# <p sourcepage="26" class="bopob_cr">Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx% <i>Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</i></p>
# <p sourcepage="26" class="bch_nmword">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</b></p>
# <p sourcepage="26" class="bch_nm">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</b></p>
# <p sourcepage="26" class="bch_tt">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</p>
# <p sourcepage="26" class="bopob_tt">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx% <b>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX% </b>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</p>
# <p sourcepage="26" class="bopob_tx">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</i></p>
# <p sourcepage="26" class="bopob_lbfirst">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</b></p>
# <p sourcepage="26" class="bopob_lb">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</b></p>
# <p sourcepage="26" class="bopob_lb">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</b></p>
# <p sourcepage="26" class="bopob_lb">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</b></p>
# [~]$
# [~]$ cat chapter1465642603/reader_1.html
# <p sourcepage="26" class="bch_ha">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</b></p>
# <p sourcepage="26" class="bopob_lblast">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</b> </p>
# <p sourcepage="26" class="bopcs_txfirst">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</p>
# <p sourcepage="26" class="bopcs_tx">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</p>
# <p sourcepage="26" class="bopcs_tx">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</p>
# <p sourcepage="27" class="bopcs_tx">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</p>
# <p sourcepage="27" class="bopcs_tx">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%<span class="sup">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</sup>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</p>
# <p sourcepage="27" class="bch_txfirst">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%</p>
# [~]$

Related

search and update string in all files using perl

Need to search and replace the string in multiple file of same extension using perl.
I am using this code, but it is not working as expected. Can anyone please help.
#!/usr/bin/perl
use strict;
use warnings;
use English;
use Tie::File;
my $string2 = 'li role="prep" class="active"'; #old string
my $string3 = 'li role="presentation"'; #new string
my $string6 = 'div role="tabpanel" class="tab-pane active" id="documentation"'; #old string
my $string7 = 'div role="tabpanel" class="tab-pane" id="documentation"'; #new string
my $quoted_substring = quotemeta($string2);
my $quoted_substring2 = quotemeta($string6);
my $dir = 'C:\Users\vkpal\Desktop\New_Report';
foreach my $fp (glob("$dir/*.html")) {
chomp($fp);
printf "%s\n", $fp;
open my $fh, "<", $fp or die "can't read open '$fp': $OS_ERROR";
while (<$fh>) {
#printf " %s", $_;
s/$quoted_substring/$string3/g;
s/$quoted_substring2/$string7/g;
}
close $fh or die "can't read close '$fp': $OS_ERROR";
}
I am using above code but it is not replacing the string. Can anyone please me here.

Find and Replace multiple texts from file a in file b

I have a csv file which has on column A (chinese phrases) and on column B (their english equivalents).
On another .xml file I have large amounts of chinese texts. I want to make the script to search from csv file and if found replace the terms on xml file.
The closest that I got to my aim is this code.
use Cwd;
use File::Basename;
use File::Copy;
use strict;
use warnings;
my $DIR = $0;
my $filename = basename($0);
$DIR = '/Users/moody/Desktop/chinese/';
#my $tablecounter = 0;
my #GLOSSARY;
my $glossaryFile = 'Chinese.csv';
my $glossaryCount = 1;
my $RAWFILE = "Chinese.xml";
my #MODDEDFILES = ('Chinese-modded.html');
BuildGlossary();
my $FILETEXT = "";
my #MODDED_FILETEXT = ('','','','','','','','','');
ReadRaw($RAWFILE);
my $k = 1;
while ($k < $glossaryCount) {
my $search = $GLOSSARY[$k][0];
my $replace = $GLOSSARY[$k][1];
$FILETEXT =~ s/$search/$replace/g;
$k += 1;
}
$FILETEXT =~ s/<p><\/p>/<\/br>/g;
PrintText($MODDEDFILES[0]);
SplitText();
$k = 1;
while ($k < 3) {
PrintTextModded($MODDEDFILES[$k], $k);
$k += 1;
}
exit;
sub PrintText {
my $filename = $_[0];
open(my $fh, '>:encoding(UTF-8)', $filename)
or die "Could not open file '$filename' $!";
print $fh "<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><head><style>body {background-color: #bfbfbf; margin-right: 20%; margin-left: 20%;} p {margin: 0px; text-indent: 1.5em; word-wrap: break-word;} h3 {margin: 0px; text-align: center;} hr {border-color:black;}</style></head><body>\n\n";
print $fh $FILETEXT;
print $fh "</body></html>\n";
close $fh;
}
sub SplitText {
my #temp = split(/(<p>------------------------------------<\/p>)/, $FILETEXT);
shift #temp;
my $tempSize = #temp;
for (my $i=1; $i <= $tempSize; $i+=4) {
$temp[$i] =~ s/<p>/<h3>/g;
$temp[$i] =~ s/<\/p>/<\/h3>/g;
}
for (my $i=0; $i <= $tempSize; $i+=2) {
$temp[$i] = "<hr noshade>\n";
}
my #volumes = ([0,111],[112,224]);
for (my $k=0; $k < 2; $k++) {
my $temptext = "";
my $i = int($volumes[$k][0]);
my $u = $volumes[$k][1];
for (my $j=$i; $j <= $u; $j++) {
$temptext = $temptext.$temp[$j];
#$temp[$j] = "";
}
$MODDED_FILETEXT[$k] = $temptext;
}
}
sub PrintTextModded {
my $filename = $_[0];
my $num = $_[1];
open(my $fh, '>:encoding(UTF-8)', $filename)
or die "Could not open file '$filename' $!";
print $fh "<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><head><style>body {background-color: #bfbfbf; margin-right: 20%; margin-left: 20%;} p {margin: 0px; text-indent: 1.5em; word-wrap: break-word;} h3 {margin: 0px; text-align: center;} hr {border-color:black;}</style></head><body>\n\n";
print $fh $MODDED_FILETEXT[$num-1];
print $fh "</body></html>\n";
close $fh;
}
sub ReadRaw {
my $filename = $_[0];
open(my $fh, '<:encoding(UTF-8)', $filename)
or die "Could not open file '$filename' $!";
$FILETEXT = "";
while (my $row = <$fh>) {
chomp $row;
$FILETEXT = $FILETEXT."<p>".$row."</p>\n";
}
close $fh;
}
sub BuildGlossary {
open(my $fh, '<:encoding(UTF-8)', $glossaryFile)
or die "Could not open file '$glossaryFile' $!";
my $num = 1;
while (my $row = <$fh>) {
chomp $row;
my #temp = split "," , $row;
$GLOSSARY[$num][0] = $temp[0];
$GLOSSARY[$num][1] = $temp[1];
$num += 1;
$glossaryCount += 1;
}
close $fh;
}
However, the problem is that it still doesn't work. The problems that I face at the moment are:
Use of uninitialized value in concatenation (.) or string at find.pl line 74.
Use of uninitialized value $filename in open at find.pl line 85.
Use of uninitialized value $filename in concatenation (.) or string at find.pl line 85.
Could not open file '' No such file or directory at find.pl line 85.
Is there anyone who could help me out?
The issue was with $DIR = '/Users/moody/Desktop/chinese/';
It had to be changed to $DIR =~ s/$FILENAME//g;

How can i convert multiple text files into same filename with .html files format using perl?

Code which i have tried for single file:
#!/usr/bin/perl
use strict;
use warnings;
open my $HTML, '>', 'h.html' or die $!;
print $HTML <<'_END_HEADER_';
<html>
<head><title></title></head>
<body>
_END_HEADER_
open my $IN, '<', 'h.txt' or die $!;
while (my $line = <$IN>) {
# You probably want to do more work here.
print $HTML $line;
}
print $HTML '</body></html>';
close $HTML or die $!;
Query:
The above code converts .txt to .html file format.In this code i have tried for single file.I got struck with how can i do for multiple files from the folder using perl?
Input files:
h.txt
e.txt
l.txt
Expected output;(With html formt)
h.html
e.html
l.html
Try glob. It return the list of filename for mentioned directory.
#!/usr/bin/perl
use strict;
use warnings;
open my $HTML, '>', 'h.html' or die $!;
print $HTML <<'_END_HEADER_';
<html>
<head><title></title></head>
<body>
_END_HEADER_
while (my $file= glob("~/Desktop/*.txt"))
{
open my $IN, '<', $file or die $!;
while (my $line = <$IN>)
{
print $HTML $line;
}
}
print $HTML '</body></html>';
close $HTML or die $!;
Writing into new file
#!/usr/bin/perl
use strict;
use warnings;
my $HTML;
#open my $HTML, '>', 'h.html' or die $!;
while (my $file= glob("~/Desktop/*.txt"))
{
open my $IN, '<', $file or die $!;
my $new_file_name = $file;
$new_file_name=~s/.+\///;
open $HTML, '>', "$new_file_name.html" or die $!;
print $HTML "
<html>
<head><title></title></head>
<body>";
while (my $line = <$IN>)
{
print $HTML $line;
}
print $HTML '</body></html>';
close $HTML;
}

Extract Contents from HTML Table [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Extract Table Contents using Perl
I am trying to extract table content from a html file using HTML::TableExtract. My problem is my html file is structured in the following way:
!DOCTYPE html>
<html>
<body>
<h4>One row and three columns:</h4>
<table border="1">
<tr>
<td>
<p> 100 </p></td>
<td>
<p> 200 </p></td>
<td>
<p> 300 </p></td>
</tr>
<tr>
<td>
<p> 100 </p></td>
<td>
<p> 200 </p></td>
<td>
<p> 300 </p></td>
</tr>
</table>
</body>
Because of this structure, my output looks like:
100|
200|
300|
400|
500|
600|
Instead of what I want:
100|200|300|
400|500|600|
Can you please help? Here is my perl code
use strict;
use warnings;
use HTML::TableExtract;
my $te = HTML::TableExtract->new();
$te->parse_file('Table_One.html');
open (DATA2, ">TableOutput.txt")
or die "Can't open file";
foreach my $ts ($te->tables()) {
foreach my $row ($ts->rows()) {
my $Final = join('|', #$row );
print DATA2 "$Final";
}
}
close (DATA2);
sub trim(_) { my ($s) = #_; $s =~ s/^\s+//; $s =~ s/\s+\z//; $s }
foreach my $ts ($te->tables()) {
foreach my $row ($ts->rows()) {
print DATA2 join('|', map trim, #$row), "\n";
}
} ^
|
|
Or if you really want the trailing "|",
sub trim(_) { my ($s) = #_; $s =~ s/^\s+//; $s =~ s/\s+\z//; $s }
foreach my $ts ($te->tables()) {
foreach my $row ($ts->rows()) {
print DATA2 (map { trim($_).'|' } #$row), "\n";
}
}

cgi perl script to list subdirectories and files

I searched from internet, but I only found php solutions to this problem. Please help if you know how to do this in perl.
I am trying to generate a webpage showing the content of a directory on my server's local disk. For example, a page containing the following will do the work
<file name="file1" href="file1" />
<dir name="dir1" href="dir1/" />
<dir name="dir2" href="dir2/" />
Thank you for your help.
Modifications have to be made to secure the script and also in jailing it. However, the idea can be implemented like:
#!/usr/bin/perl -T
use strict;
use warnings;
use CGI;
use File::Basename;
use File::Spec;
use Path::Trim;
my $cgi = CGI->new();
if ( my $file = $cgi->param('file') ) {
open my $fh, '<', $file or die $!;
print $cgi->header(
'-type' => 'application/octet-stream',
'-attachment' => basename($file),
'-Content_Length' => -s $file,
);
binmode $fh;
print while <$fh>;
}
else {
my $path = $cgi->param('path');
print $cgi->header(), $cgi->start_html();
# remove redundant current directory and parent directory entries
my $pt = Path::Trim->new();
$pt->set_directory_separator('/');
$path = $pt->trim_path($path);
# remove all ../ and ./ that have accumulated at the beginning of the path and
# make the path absolute by prepending a /
$path =~ s{^ (\.\.? /)+ }{}x;
$path = "/$path" unless $path =~ m{^ / }x;
print $cgi->h1($path);
opendir my $dh, $path or die $!;
my #entries = grep { $_ !~ /^ \. $/x } readdir $dh;
closedir $dh;
print $cgi->start_ul();
for my $entry ( sort { $a cmp $b } #entries ) {
if ( -d File::Spec->catfile( $path, $entry ) ) {
my $abs_entry = File::Spec->catfile( $path, $entry );
my $anchor = $cgi->a( { 'href' => "?path=$abs_entry" }, $entry );
print $cgi->li($anchor);
}
else {
my $abs_entry = File::Spec->catfile( $path, $entry );
my $anchor = $cgi->a( { 'href' => "?file=$abs_entry" }, $entry );
print $cgi->li($anchor);
}
}
print $cgi->end_ul(), $cgi->end_html();
}
This should get you started:
#!/usr/bin/perl -Tw
use 5.008_001;
use strict;
use warnings;
print "Content-Type: text/html; charset=utf-8\r\n\r\n";
print <<EOF;
<html>
<head>
<title>Directory Listing</title>
</head>
<body>
<pre>
EOF
opendir(my $dir, ".");
foreach(sort readdir $dir) {
my $isDir = 0;
$isDir = 1 if -d $_;
$_ =~ s/&/&/g;
$_ =~ s/"/"/g;
$_ =~ s/</</g;
$_ =~ s/>/>/g;
my $type = "[ ]";
$type = "[D]" if $isDir;
print "$type$_\n";
}
print <<EOF;
</pre>
</body>
</html>
EOF