| 1 |
NAME |
| 2 |
|
| 3 |
CGI::Compile - Compile .cgi scripts to a code reference like |
| 4 |
ModPerl::Registry |
| 5 |
|
| 6 |
SYNOPSIS |
| 7 |
|
| 8 |
use CGI::Compile; |
| 9 |
my $sub = CGI::Compile->compile("/path/to/script.cgi"); |
| 10 |
|
| 11 |
DESCRIPTION |
| 12 |
|
| 13 |
CGI::Compile is a utility to compile CGI scripts into a code reference |
| 14 |
that can run many times on its own namespace, as long as the script is |
| 15 |
ready to run on a persistent environment. |
| 16 |
|
| 17 |
NOTE: for best results, load CGI::Compile before any modules used by |
| 18 |
your CGIs. |
| 19 |
|
| 20 |
RUN ON PSGI |
| 21 |
|
| 22 |
Combined with CGI::Emulate::PSGI, your CGI script can be turned into a |
| 23 |
persistent PSGI application like: |
| 24 |
|
| 25 |
use CGI::Emulate::PSGI; |
| 26 |
use CGI::Compile; |
| 27 |
|
| 28 |
my $cgi_script = "/path/to/foo.cgi"; |
| 29 |
my $sub = CGI::Compile->compile($cgi_script); |
| 30 |
my $app = CGI::Emulate::PSGI->handler($sub); |
| 31 |
|
| 32 |
# $app is a PSGI application |
| 33 |
|
| 34 |
CAVEATS |
| 35 |
|
| 36 |
If your CGI script has a subroutine that references the lexical scope |
| 37 |
variable outside the subroutine, you'll see warnings such as: |
| 38 |
|
| 39 |
Variable "$q" is not available at ... |
| 40 |
Variable "$counter" will not stay shared at ... |
| 41 |
|
| 42 |
This is due to the way this module compiles the whole script into a big |
| 43 |
sub. To solve this, you have to update your code to pass around the |
| 44 |
lexical variables, or replace my with our. See also |
| 45 |
http://perl.apache.org/docs/1.0/guide/porting.html#The_First_Mystery |
| 46 |
for more details. |
| 47 |
|
| 48 |
METHODS |
| 49 |
|
| 50 |
new |
| 51 |
|
| 52 |
Does not need to be called, you only need to call it if you want to set |
| 53 |
your own namespace_root for the generated packages into which the CGIs |
| 54 |
are compiled into. |
| 55 |
|
| 56 |
Otherwise you can just call "compile" as a class method and the object |
| 57 |
will be instantiated with a namespace_root of CGI::Compile::ROOT. |
| 58 |
|
| 59 |
You can also set return_exit_val, see "RETURN CODE" for details. |
| 60 |
|
| 61 |
Example: |
| 62 |
|
| 63 |
my $compiler = CGI::Compile->new(namespace_root => 'My::CGIs'); |
| 64 |
my $cgi = $compiler->compile('/var/www/cgi-bin/my.cgi'); |
| 65 |
|
| 66 |
compile |
| 67 |
|
| 68 |
Takes either a path to a perl CGI script or a source code and some |
| 69 |
other optional parameters and wraps it into a coderef for execution. |
| 70 |
|
| 71 |
Can be called as either a class or instance method, see "new" above. |
| 72 |
|
| 73 |
Parameters: |
| 74 |
|
| 75 |
* $cgi_script |
| 76 |
|
| 77 |
Path to perl CGI script file or a scalar reference that contains the |
| 78 |
source code of CGI script, required. |
| 79 |
|
| 80 |
* $package |
| 81 |
|
| 82 |
Optional, package to install the script into, defaults to the path |
| 83 |
parts of the script joined with _, and all special characters |
| 84 |
converted to _%2x, prepended with CGI::Compile::ROOT::. |
| 85 |
|
| 86 |
E.g.: |
| 87 |
|
| 88 |
/var/www/cgi-bin/foo.cgi |
| 89 |
|
| 90 |
becomes: |
| 91 |
|
| 92 |
CGI::Compile::ROOT::var_www_cgi_2dbin_foo_2ecgi |
| 93 |
|
| 94 |
Returns: |
| 95 |
|
| 96 |
* $coderef |
| 97 |
|
| 98 |
$cgi_script or $$code compiled to coderef. |
| 99 |
|
| 100 |
SCRIPT ENVIRONMENT |
| 101 |
|
| 102 |
ARGUMENTS |
| 103 |
|
| 104 |
Things like the query string and form data should generally be in the |
| 105 |
appropriate environment variables that things like CGI expect. |
| 106 |
|
| 107 |
You can also pass arguments to the generated coderef, they will be |
| 108 |
locally aliased to @_ and @ARGV. |
| 109 |
|
| 110 |
BEGIN and END blocks |
| 111 |
|
| 112 |
BEGIN blocks are called once when the script is compiled. END blocks |
| 113 |
are called when the Perl interpreter is unloaded. |
| 114 |
|
| 115 |
This may cause surprising effects. Suppose, for instance, a script that |
| 116 |
runs in a forking web server and is loaded in the parent process. END |
| 117 |
blocks will be called once for each worker process and another time for |
| 118 |
the parent process while BEGIN blocks are called only by the parent |
| 119 |
process. |
| 120 |
|
| 121 |
%SIG |
| 122 |
|
| 123 |
The %SIG hash is preserved meaning the script can change signal |
| 124 |
handlers at will. The next invocation gets a pristine %SIG again. |
| 125 |
|
| 126 |
exit and exceptions |
| 127 |
|
| 128 |
Calls to exit are intercepted and converted into exceptions. When the |
| 129 |
script calls exit 19 and exception is thrown and $@ contains a |
| 130 |
reference pointing to the array |
| 131 |
|
| 132 |
["EXIT\n", 19] |
| 133 |
|
| 134 |
Naturally, "$^S" in perlvar (exceptions being caught) is always true |
| 135 |
during script runtime. |
| 136 |
|
| 137 |
If you really want to exit the process call CORE::exit or set |
| 138 |
$CGI::Compile::USE_REAL_EXIT to true before calling exit: |
| 139 |
|
| 140 |
$CGI::Compile::USE_REAL_EXIT = 1; |
| 141 |
exit 19; |
| 142 |
|
| 143 |
Other exceptions are propagated out of the generated coderef. The |
| 144 |
coderef's caller is responsible to catch them or the process will exit. |
| 145 |
|
| 146 |
Return Code |
| 147 |
|
| 148 |
The generated coderef's exit value is either the parameter that was |
| 149 |
passed to exit or the value of the last statement of the script. The |
| 150 |
return code is converted into an integer. |
| 151 |
|
| 152 |
On a 0 exit, the coderef will return 0. |
| 153 |
|
| 154 |
On an explicit non-zero exit, by default an exception will be thrown of |
| 155 |
the form: |
| 156 |
|
| 157 |
exited nonzero: <n> |
| 158 |
|
| 159 |
where n is the exit value. |
| 160 |
|
| 161 |
This only happens for an actual call to "exit" in perfunc, not if the |
| 162 |
last statement value is non-zero, which will just be returned from the |
| 163 |
coderef. |
| 164 |
|
| 165 |
If you would prefer that explicit non-zero exit values are returned, |
| 166 |
rather than thrown, pass: |
| 167 |
|
| 168 |
return_exit_val => 1 |
| 169 |
|
| 170 |
in your call to "new". |
| 171 |
|
| 172 |
Alternately, you can change this behavior globally by setting: |
| 173 |
|
| 174 |
$CGI::Compile::RETURN_EXIT_VAL = 1; |
| 175 |
|
| 176 |
Current Working Directory |
| 177 |
|
| 178 |
If CGI::Compile->compile was passed a script file, the script's |
| 179 |
directory becomes the current working directory during the runtime of |
| 180 |
the script. |
| 181 |
|
| 182 |
NOTE: to be able to switch back to the original directory, the compiled |
| 183 |
coderef must establish the current working directory. This operation |
| 184 |
may cause an additional flush operation on file handles. |
| 185 |
|
| 186 |
STDIN and STDOUT |
| 187 |
|
| 188 |
These file handles are not touched by CGI::Compile. |
| 189 |
|
| 190 |
The DATA file handle |
| 191 |
|
| 192 |
If the script reads from the DATA file handle, it reads the __DATA__ |
| 193 |
section provided by the script just as a normal script would do. Note, |
| 194 |
however, that the file handle is a memory handle. So, fileno DATA will |
| 195 |
return -1. |
| 196 |
|
| 197 |
CGI.pm integration |
| 198 |
|
| 199 |
If the subroutine CGI::initialize_globals is defined at script runtime, |
| 200 |
it is called first thing by the compiled coderef. |
| 201 |
|
| 202 |
PROTECTED METHODS |
| 203 |
|
| 204 |
These methods define some of the internal functionality of CGI::Compile |
| 205 |
and may be overloaded if you need to subclass this module. |
| 206 |
|
| 207 |
_read_source |
| 208 |
|
| 209 |
Reads the source of a CGI script. |
| 210 |
|
| 211 |
Parameters: |
| 212 |
|
| 213 |
* $file_path |
| 214 |
|
| 215 |
Path to the file the contents of which is to be read. |
| 216 |
|
| 217 |
Returns: |
| 218 |
|
| 219 |
* $source |
| 220 |
|
| 221 |
The contents of the file as a scalar string. |
| 222 |
|
| 223 |
_build_subname |
| 224 |
|
| 225 |
Creates a package name and coderef name into which the CGI coderef will |
| 226 |
be compiled into. The package name will be prepended with |
| 227 |
$self-{namespace_root}>. |
| 228 |
|
| 229 |
Parameters: |
| 230 |
|
| 231 |
* $file_path |
| 232 |
|
| 233 |
The path to the CGI script file, the package name is generated based |
| 234 |
on this path. |
| 235 |
|
| 236 |
Returns: |
| 237 |
|
| 238 |
* $package |
| 239 |
|
| 240 |
The generated package name. |
| 241 |
|
| 242 |
* $subname |
| 243 |
|
| 244 |
The generated coderef name, based on the file name (without |
| 245 |
directory) of the CGI file path. |
| 246 |
|
| 247 |
_eval |
| 248 |
|
| 249 |
Takes the generated perl code, which is the contents of the CGI script |
| 250 |
and some other things we add to make everything work smoother, and |
| 251 |
returns the evaluated coderef. |
| 252 |
|
| 253 |
Currently this is done by writing out the code to a temp file and |
| 254 |
reading it in with "do" in perlfunc so that there are no issues with |
| 255 |
lexical context or source filters. |
| 256 |
|
| 257 |
Parameters: |
| 258 |
|
| 259 |
* $code |
| 260 |
|
| 261 |
The generated code that will make the coderef for the CGI. |
| 262 |
|
| 263 |
Returns: |
| 264 |
|
| 265 |
* $coderef |
| 266 |
|
| 267 |
The coderef that is the resulting of evaluating the generated perl |
| 268 |
code. |
| 269 |
|
| 270 |
AUTHOR |
| 271 |
|
| 272 |
Tatsuhiko Miyagawa <[email protected]> |
| 273 |
|
| 274 |
CONTRIBUTORS |
| 275 |
|
| 276 |
Rafael Kitover <[email protected]> |
| 277 |
|
| 278 |
Hans Dieter Pearcey <[email protected]> |
| 279 |
|
| 280 |
kocoureasy <[email protected]> |
| 281 |
|
| 282 |
Torsten Förtsch <[email protected]> |
| 283 |
|
| 284 |
Jörn Reder <[email protected]> |
| 285 |
|
| 286 |
Pavel Mateja <[email protected]> |
| 287 |
|
| 288 |
lestrrat <[email protected]> |
| 289 |
|
| 290 |
COPYRIGHT & LICENSE |
| 291 |
|
| 292 |
Copyright (c) 2023 Tatsuhiko Miyagawa <[email protected]> |
| 293 |
|
| 294 |
This library is free software; you can redistribute it and/or modify it |
| 295 |
under the same terms as Perl itself. |
| 296 |
|
| 297 |
SEE ALSO |
| 298 |
|
| 299 |
ModPerl::RegistryCooker CGI::Emulate::PSGI |
| 300 |
|