20. PerlScript
Contents:
PerlScript is an ActiveX scripting engine that allows you to incorporate Perl within any ActiveX scripting host. PerlScript can be used on the client side (Internet Explorer 4.0) or in Active Server Pages (ASP) generated by web servers like IIS or WebSite Pro. To use PerlScript with these applications, you must have Active State's ActivePerl with PerlScript installed on the local machine (or you can use a network-based installation of each). Since a local Perl and PerlScript installation is required, it is not practical to deploy applications that use client-side PerlScript across the Internet. Nevertheless, in situations where you have control over the desktop configurations, as in an Intranet environment, client-side PerlScript offers tremendous opportunities for browser-based application development. Server-side PerlScript does not have this limitation. To use PerlScript on your web server, you don't need to worry about how the end user's desktop is configured. As long as you are running a web server that can use ASP, you can use PerlScript on your web server. With the free distribution of Netscape Navigator source code, the availability of PerlScript is likely to broaden significantly. This is an area of development that you should keep an eye on if you like the idea of extending Perl's power to web authoring. PerlScript is implemented into HTML pages or ASP pages via the Document Object Model used by the ActiveX scripting engine. Scripts are virtually the same as any other Perl script. They use Perl's object-oriented features on an object hierarchy defined by the script engine. The object model is different for clients and servers, but the same scheme is used throughout: top-level objects with sub-objects, each containing their own properties and methods. Properties of objects, like the name of a frame or the background color of a window, are available as hash elements from a referenced object. For example: Thus, properties can be set or retrieved.$object->subobject{'property'} = "value"; $val = $object->subobject->{'property'}; Objects have a predefined set of methods available to them as well. Methods define actions such as writing to a document or performing actions on a mouse click. Methods are accessible in the usual way: This chapter provides information on the parts of the object model that you will use most often. A complete reference for the object models for both ASP and ActiveX scripting clients is beyond the scope of this book. For more detailed information, consult the documentation for your server, or the Microsoft web site, which has complete client and server information.$object->method(args); 20.1 Client-Side PerlScript
All PerlScript code must be contained within a The<HTML> <HEAD> <TITLE>Hello, World</TITLE> </HEAD> <BODY> <H1>Hello, from the Old Edition!</H1> <SCRIPT LANGUAGE="PerlScript"> my @one_hit = ('Bel', 'Biv', 'DeVoe'); foreach (@one_hit) { $window->document->write(qq[$_ says "Hello"!<BR>]); } $window->document->write(qq[<P><I>"That girl is Poison!"</I></P>]); </SCRIPT> </BODY> </HTML>
write
method is used on the document object to "write" HTML and text
to the document displayed in the window.
The top-level object is the window. This is the default object that contains the script. (Even though the script is contained in the HTML file, the script object is a level below the window, just like the document object.) Every window contains the following objects:
The most important object in this hierarchy is the document object. The following section explains the objects it contains, their properties, and their methods. 20.1.1 The Document Object
The document object represents what is displayed within the client window. In the
example above, we showed that the
The anchor and link objects both provide arrays. Each element in the array is a string with the name of the anchor or location of the hyperlink. The elements in the link array are read-only. Anchor names can be manipulated by the script. The form object provides access to the elements of the forms within your document. Each form is an indexed element of the form object array and can be accessed like this: This would be the second form in the document. Under this form object are objects representing the various parameters of the form and input tags. They are accessible by the names of the attributes used in the HTML tags.$obj = $window->document->form[1]; 20.1.1.1 Document methods
The following methods can be used on the document object.
They are accessed using the usual object-method syntax:
20.1.1.2 Document propertiesThe document object contains a set of properties described below. Properties can be set or retrieved, and are accessed as hash variables under the document object. They are accessible via the following syntax: $val = $window->document->{property};
Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|