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