Posted: . At: 9:09 PM. This was 12 years ago. Post ID: 4504
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.

Miscellaneous Perl programming information. How to use for loops and printing HTML properly.

Opening a folder and listing the contents, and not listing certain files. This is the Perl code I was using on my very old Tripod.com website.

opendir(DNAME, "$folder") || die "I cannot open the requested directory $folder \n $!";
@dirfiles2 = readdir (DNAME);
@dirfiles2 = sort(@dirfiles2);
foreach $x2 (@dirfiles2) {
    if ($x2 eq ".") {
	$x2 = ' ';
    }
    if ($x2 eq "..") {
	$x2 = ' ';
    }
    if ($x2 =~ /REMOVED/) {
	$x2 = ' ';
    }
    if ($x2 eq ".cgiclean") { # What is this?
	$x2 = ' ' ;
    }
    if ($x2 eq ".htaccess") {
	$x2 = ' ';
    }
    if ($x2 eq "$archive") {
	print "<OPTION selected value=\"$folder\/$archive\" ID='formstuff'>$archive</OPTION>\n";
    } else {
# Simple hack to weed out those annoying zero length() strings...
	if (length $x2 > 3) {
	    print "<OPTION value=\"$folder\/$x2\" ID='formstuff'>$x2</OPTION>\n";
	}
    }
}
 
closedir(DNAME);

How to get the date and time with Perl scripting. All you need to do is print “$date”; and you are done.

my @Days = ('Sunday','Monday','Tuesday',
	    'Wednesday','Thursday','Friday',
	    'Saturday');
my @Mon1 = ('January','February','March',
	    'April','May','June',
	    'July','August','September',
	    'October','November','December');
 
my ($Sec,$Min,$Hour,$MDay,$Mon,$Year,$WkDay) = (localtime)[0,1,2,3,4,5,6];
$Year += 1900;
if($Sec < 10) {
    $Sec = "0$Sec"
};
if($Min < 10) {
    $Min = "0$Min"
};
if($Hour < 10) {
    $Hour = "0$Hour"
};
 
$date = "$Hour:$Min:$Sec - $Days[$WkDay] $MDay $Mon1[$Mon] $Year";

Here is an example showing how to do a for() loop in Perl.

for ( $i=  0; $i <  32; $i++) {
	print "*";
}

This comprehensive script will print a HTML page to the terminal when you run it. This is a good starter for writing a CGI script that will print out its own HTML. This is using the CGI.pm module. This also sets a cookie containing the date that expires in 3 days.

#!/usr/bin/perl
 
use CGI qw/:standard -nosticky/;
my $query = new CGI;
 
print $query->header(-type=>'text/html',
		     -expires=>'+3d',
		     -cookie=>$date,
		     -charset=>'UTF-8');
 
print $query->start_html(-title=>'CGI Viewblog CGI Script.',
			 -meta=>{'keywords'=>'Blog Doom2 Level Editing Linux Doom',
				 'copyright'=>'Copyright 2004 John Cartwright.',
				 'definition'=>'General Viewblog Script.',
				 'Appeal'=>'Wide',
				 'description'=>'CGI Viewblog Script.'},
			 -BGCOLOR=>'#FFFFFF',
			 -TEXT=>'#000000',
			 -LINK=>'#888fff');
 
print $query->h1("CGI HTML Script.\n");
 
print $query->p("This script prints HTML to the terminal.\n");
 
print $query->end_html;

Finally, this code will write some information to a log file.

open FILE, ">> log.txt" || die "I cannot open the file: log.txt:\n $!\n";
print FILE "Visited: $date\n";
if ($user_ref) {
    print FILE "Referrer URL: $user_ref.\n";
}
print FILE "Users Browser: $user_agent.--\n\n";
close FILE;

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.