PREVIOUS  TABLE OF CONTENTS  NEXT 

PerlScript

Jim Esten

There is little disputing the widespread use of Perl for server-side processing on the web. As many as 90% of the dynamic pages on the web are activated with Perl. We probably shouldn't be greedy and want the other 10%, but what about pages that don't use any backend processing? Enter PerlScript. What is PerlScript?

PerlScript is the latest addition to the Win32 Perl family from ActiveState (http://www.activestate.com). Released in beta on December 4, 1996, it's an ActiveX scripting engine that allows you to write PerlScript code for any ActiveX host - both for servers and browsers. I'll forgo the operating system wars (there aren't yet any UNIX-based web server ActiveX hosts!) and limit the discussion to the browser side. Here, PerlScript stands up to be counted alongside JavaScript from Netscape and VBScript from Microsoft.

Web scripting engines are usually stripped down versions of a programming language. VBScript has much of the same syntax and functions as Visual Basic, even sharing online documentation. Javascript is syntactically similar to C. PerlScript has its roots in our beloved Perl. What they all tend to share are some fundamental limitations put in place for security reasons. Most apparent is the lack of file operations and access to system commands. Even this shouldn't appease the security-minded, since like all ActiveX controls, PerlScript can read your registry and launch OLE applications.

There is currently a larger downside for PerlScript: the only browser family supporting it is Microsoft's Internet Explorer, (version 3.0 and higher). Without passing judgement, I will note only that PerlScript isn't even mentioned on Netscape's web site, so you'll need Internet Explorer to run the examples in this article. The PerlScript distribution contains some Explorer based examples. Documentation is scarce, but armed with the examples and the ever-friendly Programming Perl, you shouldn't have any trouble.

Why PerlScript?

Scripting engines for browsers are used for many purposes. In my own applications-driven intranet, scripting is used mostly for validating form inputs. We use web forms to create "input decks" for legacy FORTRAN programs that don't tolerate less than perfect input. Elsewhere, scripts are used for everything from databases to page animations (clocks, countdowns, ticker tapes, and the like).

Enough Already...Show Me!

Let's start with the obligatory welcome message. For those not familiar with combining script with HTML, two common methods are employed. Most common is placing all script functions between <head><script> and </script></head> tags. This method preloads all functions so that they are available to the body of the HTML page. Single functions are often invoked with the <body onLoad=function> tag:

<html>
<head>
<title>A Simple PerlScript Greeting</title>
<script language="PerlScript">

sub ShowGreeting { 
$window->document->write("Greetings, Perl Hacker!<br>");
}
</script> 
</head>
<body onLoad="ShowGreeting()"> 
</body> 
</html>

Figure 1

Figure 1: Message generated with PerlScript subroutine.

PerlScript handles the window object differently than JavaScript and VBScript. The snippet above does indeed generate the message, but the browser doesn't realize it is done loading - anything placed later in the body will only flash briefly before being overwritten by the greeting.

Figure 2

Figure 2: PerlScript in Action

Because we can mix pure HTML with the rest of our quoted string, we have a second, preferred method of executing Perl code - place the subroutine call inside the body. In the next example, we still define the subroutine in the head of the document, but then make use of script tags in the body to call the subroutine. Here's the code that generates the figure on the preceding page:

<html> 	
<head> 
<title>A Simple PerlScript Greeting</title>
<script language="PerlScript">

sub ShowGreeting { 	
    $window->document->write("Greetings, Perl Hacker!<br>"); 
}

</script> 
</head> 

<body> 
<script> 	
ShowGreeting(); 
</script> 

<p>  This message generated with a PerlScript subroutine. 
<p> 
</body>  </html> 		

Those who have used other scripting engines will note the difference in the object access syntax. In the example, PerlScript makes a distinction between $window (the object), document (a package in the window object), and write() (a method for modifying the document package). Other web scripting languages do not make a distinction in syntax. In JavaScript, the same message would be written as:

window.document.write( "Greetings, Perl Hacker! ");

with the HTML line break tag most easily placed outside the statement (you may include it, but only if you're careful!).

Figure 3

Figure 3: PerlScript in Action

Buttons

The following program/page uses several buttons to perform some string conversions. The first two buttons use the familiar tr/// operator; the second uses an idiom from Programming Perl to move the first word to the end.

<HTML> 
<HEAD> 
<TITLE>A Simple PerlScript Case Convertor</TITLE>
<SCRIPT language="PerlScript">

sub Sample::toUpper_onClick() { 	
    $input = $Sample->TheString->{'Value'}; 	
    $input =~ tr/[a-z]/[A-Z]/; 	
    $Sample->Converted->{'Value'} = $input; 	
}

sub Sample::toLower_onClick() {
    $input = $Sample->TheString->{'Value'};
    $input =~ tr/[A-Z]/[a-z]/;
    $Sample->Converted->{'Value'} = $input;
}

sub Sample::obfuscate_onClick() { 	
    $input = $Sample->TheString->{'Value'};	
    $output = join("", reverse split(/(\S+)/,
                                      $input));
    	$Sample->Converted->{'Value'} = $output; 	
}

</SCRIPT> 	
</HEAD> 
<BODY> 	<center> 	<h2>PerlScript in Action</h2> 	<hr> 
<form action="" name="Sample"> 	
<input type="text" size="70" name="TheString"> 
<p> 

<input type="button" name ="toUpper"
                    value = "Upper Case Conversion">

<input type="button" name = "toLower" 
                    value = "Lower Case Conversion">

<input type="button" name = "obfuscate" 
                    value = "Obfuscation"> 

	<p> 	<input type="text" size="80" name="Converted"> 	
</form> 
</center> 
</BODY> 
</HTML>

Web forms respond to a variety of events. Shown above is the onClick() event understood by buttons. You can have PerlScript call a method when the user moves the cursor over an area, or catch invalid input after the user presses a submit button but before the data is sent to the server, or simply warn the user that the next operation may take a long time to complete. The ActiveWare web site features a full-fledged PerlScript RPN calculator.

Nearly everything you can do in Win32 Perl is available in PerlScript. As mentioned earlier, scripting does have one major limitation - no file input/output on either the host or client machine (a notable exception is cookies [See TPJ #3 -ed]). It's a limitation I can live with; the net has become fertile ground for mischief in the past year or so.

Figure 4

Figure 4: Use of Buttons with PerlScript

One application at my company manipulates large monetary values, which are difficult to read without commas. Here's how you commaize numbers in JavaScript:

function money(inputstr) {
  var backwards = ""; 
  var backcommas = ""; 
  var outstring = "";
  dec = inputstr.indexOf("."); 
  len = inputstr.length; 	
  dollars = inputstr.substring(0,dec); 	
  commas = 0;

// turn it around....

  for (i=dec;i>=0;i--) { 		 
    backwards = backwards + dollars.charAt(i); 		 
  }

// now we're backwards, put in commas 
// every 3 digits...

  for (i=0;i<=dec;i++) { 			
    if (i % 3 == 0 && i != dec && i != 0) { 
      backcommas = backcommas + ","; commas++; 			
    } 
    backcommas = backcommas + backwards.charAt(i); 
  }
  
// and turn it back to where we started...

  for (i=dec+commas;i>=0;i--) { 
    outstring = outstring + backcommas.charAt(i) 			
  }

// and paste the decimal point and cents back on....

  finale = "\$"+outstring + inputstr.substring(dec,len);

  return finale; 		
}

The ActiveX Scripting Engine

Let's compare this to its PerlScript equivalent, and include the HTML tags just for kicks:

<head> <title>Commas PerlScript Style</title> 	
<script language="PerlScript">

sub commas { 
    local($_) = @_; 	
    1 while s/(.*\d)(\d\d\d)/$1,$2/; 		
    $_; 
}

sub doCommas_onClick() {
    $x = $AddComma->floatIn->{'Value'}; 	
    $result = commas($x); 
    $result = "\$".$result; 		
    $AddComma->floatOut->{'Value'} = $result; 		
}

</script>    </head> 

Perl's regular expressions make such programs easier to write, modify, and maintain. The more work browsers do, the less your servers have to. PerlScript on clients and Perl CGI programs on servers constitute a powerful web programming duo.

Where Next?

I was disappointed to find PerlScript support missing from the browser in Netscape's Communicator 4.01 release. If history is any indication, the leading browsers will add PerlScript and full server-side support won't be far behind.

PerlScript is still a young language, and resources are scarce. ActiveState's web site is the best starting point for information, since it has some longer PerlScript examples than you'll find here. For those just getting started with scripting, several books are available, including JavaScript: The Definitive Guide, from O'Reilly, and Danny Goodman's JavaScript Bible, from IDG Books. They don't talk specifically about PerlScript, but will familiarize you with the scripting model and how it relates to browsers, servers, and web programming in general.

_ _END_ _


Jim Esten is a web application developer at Wisconsin Electric Power.
PREVIOUS  TABLE OF CONTENTS  NEXT