1CGI::Push(3) User Contributed Perl Documentation CGI::Push(3)
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
35 animated 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
52 parameters 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 homogeneous MIME type. However if you provide either
102 of the magic values "heterogeneous" or "dynamic" (the latter
103 provided for the convenience of those who hate long parameter
104 names), you can specify the MIME type -- and other header fields --
105 on a 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 Ordinarily all pages displayed by CGI::Push share a common MIME type.
122 However by providing a value of "heterogeneous" or "dynamic" in the
123 do_push() -type parameter, you can specify the MIME type of each page
124 on a case-by-case basis.
125
126 If you use this option, you will be responsible for producing the HTTP
127 header for each page. Simply modify your draw routine to look like
128 this:
129
130 sub my_draw_routine {
131 my($q,$counter) = @_;
132 return header('text/html'), # note we're producing the header here
133 start_html('testing'),
134 h1('testing'),
135 "This page called $counter times";
136 }
137
138 You can add any header fields that you like, but some (cookies and
139 status fields included) may not be interpreted by the browser. One
140 interesting effect is to display a series of pages, then, after the
141 last page, to redirect the browser to a new URL. Because redirect()
142 does b<not> work, the easiest way is with a -refresh header field, as
143 shown below:
144
145 sub my_draw_routine {
146 my($q,$counter) = @_;
147 return undef if $counter > 10;
148 return header('text/html'), # note we're producing the header here
149 start_html('testing'),
150 h1('testing'),
151 "This page called $counter times";
152 }
153
154 sub my_last_page {
155 return header(-refresh=>'5; URL=http://somewhere.else/finished.html',
156 -type=>'text/html'),
157 start_html('Moved'),
158 h1('This is the last page'),
159 'Goodbye!'
160 hr,
161 end_html;
162 }
163
164 Changing the Page Delay on the Fly
165 If you would like to control the delay between pages on a page-by-page
166 basis, call push_delay() from within your draw routine. push_delay()
167 takes a single numeric argument representing the number of seconds you
168 wish to delay after the current page is displayed and before displaying
169 the next one. The delay may be fractional. Without parameters,
170 push_delay() just returns the current delay.
171
173 Server push scripts must be installed as no-parsed-header (NPH) scripts
174 in order to work correctly on many servers. On Unix systems, this is
175 most often accomplished by prefixing the script's name with "nph-".
176 Recognition of NPH scripts happens automatically with WebSTAR and
177 Microsoft IIS. Users of other servers should see their documentation
178 for help.
179
180 Apache web server from version 1.3b2 on does not need server push
181 scripts installed as NPH scripts: the -nph parameter to do_push() may
182 be set to a false value to disable the extra headers needed by an NPH
183 script.
184
186 Copyright 1995-1998, Lincoln D. Stein. All rights reserved.
187
188 This library is free software; you can redistribute it and/or modify it
189 under the same terms as Perl itself.
190
191 Address bug reports and comments to: lstein@cshl.org
192
194 This section intentionally left blank.
195
197 CGI::Carp, CGI
198
199
200
201perl v5.12.2 2011-01-05 CGI::Push(3)