1CGI::Push(3pm) Perl Programmers Reference Guide CGI::Push(3pm)
2
3
4
6 CGI::Push - Simple Interface to Server Push
7
9 use CGI::Push qw(:standard);
10
11 do_push(-next_page=>\&next_page,
12 -last_page=>\&last_page,
13 -delay=>0.5);
14
15 sub next_page {
16 my($q,$counter) = @_;
17 return undef if $counter >= 10;
18 return start_html('Test'),
19 h1('Visible'),"\n",
20 "This page has been called ", strong($counter)," times",
21 end_html();
22 }
23
24 sub last_page {
25 my($q,$counter) = @_;
26 return start_html('Done'),
27 h1('Finished'),
28 strong($counter - 1),' iterations.',
29 end_html;
30 }
31
33 CGI::Push is a subclass of the CGI object created by CGI.pm. It is
34 specialized for server push operations, which allow you to create ani‐
35 mated pages whose content changes at regular intervals.
36
37 You provide CGI::Push with a pointer to a subroutine that will draw one
38 page. Every time your subroutine is called, it generates a new page.
39 The contents of the page will be transmitted to the browser in such a
40 way that it will replace what was there beforehand. The technique will
41 work with HTML pages as well as with graphics files, allowing you to
42 create animated GIFs.
43
44 Only Netscape Navigator supports server push. Internet Explorer
45 browsers do not.
46
48 CGI::Push adds one new method to the standard CGI suite, do_push().
49 When you call this method, you pass it a reference to a subroutine that
50 is responsible for drawing each new page, an interval delay, and an
51 optional subroutine for drawing the last page. Other optional parame‐
52 ters include most of those recognized by the CGI header() method.
53
54 You may call do_push() in the object oriented manner or not, as you
55 prefer:
56
57 use CGI::Push;
58 $q = new CGI::Push;
59 $q->do_push(-next_page=>\&draw_a_page);
60
61 -or-
62
63 use CGI::Push qw(:standard);
64 do_push(-next_page=>\&draw_a_page);
65
66 Parameters are as follows:
67
68 -next_page
69 do_push(-next_page=>\&my_draw_routine);
70
71 This required parameter points to a reference to a subroutine
72 responsible for drawing each new page. The subroutine should
73 expect two parameters consisting of the CGI object and a counter
74 indicating the number of times the subroutine has been called. It
75 should return the contents of the page as an array of one or more
76 items to print. It can return a false value (or an empty array) in
77 order to abort the redrawing loop and print out the final page (if
78 any)
79
80 sub my_draw_routine {
81 my($q,$counter) = @_;
82 return undef if $counter > 100;
83 return start_html('testing'),
84 h1('testing'),
85 "This page called $counter times";
86 }
87
88 You are of course free to refer to create and use global variables
89 within your draw routine in order to achieve special effects.
90
91 -last_page
92 This optional parameter points to a reference to the subroutine
93 responsible for drawing the last page of the series. It is called
94 after the -next_page routine returns a false value. The subroutine
95 itself should have exactly the same calling conventions as the
96 -next_page routine.
97
98 -type
99 This optional parameter indicates the content type of each page.
100 It defaults to "text/html". Normally the module assumes that each
101 page is of a homogenous MIME type. However if you provide either
102 of the magic values "heterogeneous" or "dynamic" (the latter pro‐
103 vided for the convenience of those who hate long parameter names),
104 you can specify the MIME type -- and other header fields -- on a
105 per-page basis. See "heterogeneous pages" for more details.
106
107 -delay
108 This indicates the delay, in seconds, between frames. Smaller
109 delays refresh the page faster. Fractional values are allowed.
110
111 If not specified, -delay will default to 1 second
112
113 -cookie, -target, -expires, -nph
114 These have the same meaning as the like-named parameters in
115 CGI::header().
116
117 If not specified, -nph will default to 1 (as needed for many
118 servers, see below).
119
120 Heterogeneous Pages
121
122 Ordinarily all pages displayed by CGI::Push share a common MIME type.
123 However by providing a value of "heterogeneous" or "dynamic" in the
124 do_push() -type parameter, you can specify the MIME type of each page
125 on a case-by-case basis.
126
127 If you use this option, you will be responsible for producing the HTTP
128 header for each page. Simply modify your draw routine to look like
129 this:
130
131 sub my_draw_routine {
132 my($q,$counter) = @_;
133 return header('text/html'), # note we're producing the header here
134 start_html('testing'),
135 h1('testing'),
136 "This page called $counter times";
137 }
138
139 You can add any header fields that you like, but some (cookies and sta‐
140 tus fields included) may not be interpreted by the browser. One inter‐
141 esting effect is to display a series of pages, then, after the last
142 page, to redirect the browser to a new URL. Because redirect() does
143 b<not> work, the easiest way is with a -refresh header field, as shown
144 below:
145
146 sub my_draw_routine {
147 my($q,$counter) = @_;
148 return undef if $counter > 10;
149 return header('text/html'), # note we're producing the header here
150 start_html('testing'),
151 h1('testing'),
152 "This page called $counter times";
153 }
154
155 sub my_last_page {
156 return header(-refresh=>'5; URL=http://somewhere.else/finished.html',
157 -type=>'text/html'),
158 start_html('Moved'),
159 h1('This is the last page'),
160 'Goodbye!'
161 hr,
162 end_html;
163 }
164
165 Changing the Page Delay on the Fly
166
167 If you would like to control the delay between pages on a page-by-page
168 basis, call push_delay() from within your draw routine. push_delay()
169 takes a single numeric argument representing the number of seconds you
170 wish to delay after the current page is displayed and before displaying
171 the next one. The delay may be fractional. Without parameters,
172 push_delay() just returns the current delay.
173
175 Server push scripts must be installed as no-parsed-header (NPH) scripts
176 in order to work correctly on many servers. On Unix systems, this is
177 most often accomplished by prefixing the script's name with "nph-".
178 Recognition of NPH scripts happens automatically with WebSTAR and Mi‐
179 crosoft IIS. Users of other servers should see their documentation for
180 help.
181
182 Apache web server from version 1.3b2 on does not need server push
183 scripts installed as NPH scripts: the -nph parameter to do_push() may
184 be set to a false value to disable the extra headers needed by an NPH
185 script.
186
188 Copyright 1995-1998, Lincoln D. Stein. All rights reserved.
189
190 This library is free software; you can redistribute it and/or modify it
191 under the same terms as Perl itself.
192
193 Address bug reports and comments to: lstein@cshl.org
194
196 This section intentionally left blank.
197
199 CGI::Carp, CGI
200
201
202
203perl v5.8.8 2001-09-21 CGI::Push(3pm)