Posted: . At: 10:36 AM. This was 3 years ago. Post ID: 15096
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.


A very useful Perl coding example. How to filter swear words.


This is a very useful Perl code sample, this will filter out any bad words that are entered into the array.

1
2
3
4
5
6
7
8
9
10
11
12
my @forbidden = ("very","bad","words","go","in","this","array");
 
foreach my $forbidden (@forbidden) { # If a naughty word is typed...
	if ($comments =~ /$forbidden/) {
	goodbye2(); # Send the naughty cur to somewhere else.
	exit; # Kill the mood.
	} else { if ($realname =~ /$forbidden/) { # If they type a naughty name...
		goodbye2(); # See you later!
		exit;
		}
	}
}

This would be very useful in a web app. The goodbye2() function is what we are redirected to if we type a bad word. This can be used to admonish the user and dissuade them from this type of conduct in the future. This is a very useful example of how to check a string for certain strings.

The code below is a very useful Perl code sample to get the date and time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
=datecode
 
	Below is my date-time code. I have written some code that
	will work with the standard Perl distribution.
 
	I am very happy with the code I have written and it should work
	very well. I have used the POSIX module in the past to get the
	date and time with strftime, but this method works just as well.
 
=cut
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"};
my $mytime = sprintf("%s:%s:%s - %s %i %s %s", $Hour,$Min,$Sec, $Days[$WkDay], $MDay, $Mon1[$Mon], $Year);

The above code is pretty simple. This will print a very nice date and time string.

Below is the code I used to create a web form. This is how easy it is to render an HTML web form with the Perl CGI module.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
print $query->start_html(-title=>'Comments Guestbook',
                            -meta=>{'keywords'=>'Blog Doom2 Linux Quake Doom3',
                                    'copyright'=>'Copyright 2004 John Cartwright.',
				    'definition'=>'General Guestbook Script.',
				    'Appeal'=>'Wide',
				    'description'=>'CGI Guestbook Script.'},
                            -BGCOLOR=>'white',
		    	    -TEXT=>'black',
		    	    -LINK=>'blue');
 
print $query->h3("CGI Guestbook.\n");
	print $query->p("\nThe Time and Date is:&nbsp;$mytime.\n");
	print $query->p("Thanks for your Feedback.!\n");
print $query->start_form(
		-action=>'mybook.cgi',
		-method=>'POST',
		-comment=>'Hello.',
		-enctype=>);
print $query->p("Type your name here.\n");
print $query->textfield('realname');
print $query->br;
print $query->br;
print $query->p("And your website URL. Optional.\n");
	print $query->textfield('url');
print $query->p("Type your comments below.\n");
	print $query->br;
	print $query->textarea('comments','Please type a post.','25','65');
	print $query->br;
	print $query->br;
	print $query->submit('submit','Open Page');
	print $query->reset('reset','Reset Form');
	print $query->br;
print $query->p("Now some information about the Guestbook File.\n");
print $query->end_form();
	print $query->br;
	print $query->p("File Size is: $fsize Bytes. Verbose File Size: $verbosefsize\n");
	print $query->p("Filesize in Hex: $modehex. Filesize in Binary, $modebin.\n");
	print $query->p("Guestbook last modified: $io seconds since Epoch.\n");
print $query->end_html();

This is very easy to do indeed. It simplifies the whole process. And you may still use the content of variables to display text, the labelling of form elements is very simple.

And this is the goodbye2 function, this is used to dissuade the user from further infractions with bad language.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
sub goodbye2 {
 
print(<<"GOODBYE_DUDE");
 
	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
	<html>
	<head>
	<base target="_top">
	<meta http-equiv="Refresh" content="2; URL=$links[int rand $number])">
	<title>Guestbook Script.</title>
	</head>
	<body bgcolor="#FFFFFF" text="#000000"  link="#302c64">
	<H1>Foul Mouthed Cur!</H1>
	<BR><BR>
	<P>Goodbye and tone down your language please. $realname!</P>
	<P>You are being redirected to a random site. I hope you have something better to say next time.</P>
	<H4>Wash your mouth out with soap!</H4>
	<BR><BR>
	</body>
	</html>
 
GOODBYE_DUDE
}

Leave a Comment

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