CGI-Compile-0.25000755001750001750 013712054757 14340 5ustar00rkitoverrkitover000000000000README100644001750001750 1776413712054757 15340 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25NAME 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: 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 CONTRIBUTORS Rafael Kitover Hans Dieter Pearcey kocoureasy Torsten Förtsch Jörn Reder Pavel Mateja lestrrat COPYRIGHT & LICENSE Copyright (c) 2020 Tatsuhiko Miyagawa 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 Changes100644001750001750 607413712054757 15723 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25Revision history for Perl extension CGI::Compile 0.25 2020-08-03 18:39:36 UTC - add file name to package name #25 0.24 2020-01-30 13:59:56 UTC - use better packages/subnames for coderefs - add all CONTRIBUTORS to POD - add test for working alarm signal in CGIs 0.23 2020-01-17 10:31:33 UTC - fix race condition in temp dir creation (jr-dimedis) #24 0.22 2017-02-02 09:59:06 PST - fix tests in signal-masked environment (lemrouch) #20 0.21 2016-01-01 13:13:10 PST - fix warnings with non-numeric return values (rkitover) #18 - fix uninitialized value in open warning on perl 5.8 (rkitover) 0.20 2015-10-16 13:55:13 PDT - Compile Perl code via a tempfile to support source filters (rkitover) #17 - chain to original $SIG{__WARN__} when suppressing signal warnings on Win32 (rkitover) #16 0.19 2015-03-06 11:33:32 PST - fix signal related test fails on Win32 (rkitover) #16 0.18 2014-10-27 15:16:55 PDT - Skip tests when CGI.pm is not available #15 - Avoid CGI.pm warnings in tests 0.17 2014-05-24 11:03:19 PDT - fix %SIG localization (preserve magic) (torsten) - fix script exit code handling (torsten, rkitover) - hide my and our variables from the generated sub (torsten) - allow parameters to CGI coderef (rkitover) 0.16 Sun Mar 10 00:12:45 PST 2013 - Fixed warnings on 5.10 (rkitover) 0.15 Tue May 24 09:41:52 PDT 2011 - handle DOS line endings (rkitover) 0.14 Thu Jan 20 15:07:59 PST 2011 - Enable the warnings (rkitover) 0.13 Mon Jan 17 14:36:27 PST 2011 - Improved documents about nested closure - check -w switch on CGIs (rkitover) 0.12 Sun Aug 8 23:35:52 PDT 2010 - better mod_perl compatibility (chain to CORE::GLOBAL::exit) (rkitover) 0.11 Wed Mar 10 23:57:55 JST 2010 - Strip __END__ (lestrrat) - Refactored tests and added an unit test for __DATA__ and __END__ 0.10 Mon Feb 22 07:19:00 PST 2010 - repackaging the distribution with Module::Install 0.92 because 0.93's author_tests support is broken. https://rt.cpan.org/Public/Bug/Display.html?id=54878 0.09 Mon Feb 22 02:51:56 PST 2010 - support exit() in use'd modules (rkitover) - make compile.t run on Perl < 5.8.9 (rkitover) 0.08 Sun Jan 10 09:30:52 PST 2010 - Skip compile.t with Perl < 5.8.9 since it appears to have in %SIG restore (Andreask Koenig) 0.07 Sun Dec 27 02:56:44 JST 2009 - Fixed %SIG tests (rkitover) 0.06 Mon Dec 21 09:50:57 PST 2009 - support __DATA__ sections (rkitover) - support CGIs that call exit() (rkitover) - preserve %SIG on compile and run (rkitover) 0.05 Fri Dec 4 21:45:36 GMT 2009 - Really fix the chdir bug by saving the return value of pushd 0.04 Fri Dec 4 20:27:02 GMT 2009 - Fixed more chdir stuff using File::pushd 0.03 Fri Dec 4 19:20:20 GMT 2009 - Fixed a bug with chdir 0.02 Fri Dec 4 16:55:28 GMT 2009 - include Test::Requires 0.01 Fri Dec 4 04:56:55 2009 - original version LICENSE100644001750001750 4400713712054757 15453 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25This software is copyright (c) 2020 by Tatsuhiko Miyagawa . This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2020 by Tatsuhiko Miyagawa . This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2020 by Tatsuhiko Miyagawa . This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End cpanfile100644001750001750 107013712054757 16123 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25requires 'perl', '5.008001'; requires 'File::pushd'; requires 'Sub::Name'; on configure => sub { requires 'Module::Build::Tiny'; }; on test => sub { requires 'Test::More'; requires 'Test::NoWarnings'; requires 'Test::Requires'; requires 'Capture::Tiny'; requires 'Try::Tiny'; requires 'CGI'; requires 'Switch'; requires 'Sub::Identify'; }; on develop => sub { requires 'Dist::Zilla'; requires 'Dist::Zilla::PluginBundle::Milla'; requires 'CGI::Emulate::PSGI'; requires 'Plack::Test'; requires 'Test::Pod'; }; dist.ini100644001750001750 4413712054757 16023 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25[@Milla] GithubMeta.user = miyagawa t000755001750001750 013712054757 14524 5ustar00rkitoverrkitover000000000000CGI-Compile-0.25exit.t100644001750001750 43113712054757 16000 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/tuse strict; use Test::More tests => 2; use CGI::Compile; use Capture::Tiny 'capture_stdout'; use lib "t"; use Exit; my $sub = CGI::Compile->compile("t/exit.cgi"); my $out = capture_stdout { $sub->() }; like $out, qr/Hello/; pass "Not exiting"; Exit::main; fail "Should exit"; psgi.t100644001750001750 106113712054757 16011 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/tuse Test::More; use CGI::Compile; use CGI; use Test::Requires qw(CGI::Emulate::PSGI Plack::Test HTTP::Request::Common); use CGI::Emulate::PSGI; use Plack::Test; use HTTP::Request::Common; my $sub = CGI::Compile->compile("t/hello.cgi"); my $app = CGI::Emulate::PSGI->handler($sub); test_psgi app => $app, client => sub { my $cb = shift; my $res = $cb->(GET "http://localhost/?name=foo"); is $res->content, "Hello foo counter=1"; $res = $cb->(GET "http://localhost/?name=bar"); is $res->content, "Hello bar counter=2"; }; done_testing; Build.PL100644001750001750 26113712054757 15674 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25# This Build.PL for CGI-Compile was generated by Dist::Zilla::Plugin::ModuleBuildTiny 0.015. use strict; use warnings; use 5.008001; use Module::Build::Tiny 0.034; Build_PL(); META.yml100644001750001750 265513712054757 15702 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25--- abstract: 'Compile .cgi scripts to a code reference like ModPerl::Registry' author: - 'Tatsuhiko Miyagawa ' build_requires: CGI: '0' Capture::Tiny: '0' Sub::Identify: '0' Switch: '0' Test::More: '0' Test::NoWarnings: '0' Test::Requires: '0' Try::Tiny: '0' configure_requires: Module::Build::Tiny: '0.034' dynamic_config: 0 generated_by: 'Dist::Milla version v1.0.20, Dist::Zilla version 6.015, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: CGI-Compile no_index: directory: - eg - examples - inc - share - t - xt requires: File::pushd: '0' Sub::Name: '0' perl: '5.008001' resources: bugtracker: https://github.com/miyagawa/CGI-Compile/issues homepage: https://github.com/miyagawa/CGI-Compile repository: https://github.com/miyagawa/CGI-Compile.git version: '0.25' x_contributors: - 'Hans Dieter Pearcey ' - 'Jörn Reder ' - 'kocoureasy ' - 'lestrrat ' - 'Pavel Mateja ' - 'Rafael Kitover ' - 'Rafael Kitover ' - 'Torsten Förtsch ' x_generated_by_perl: v5.30.3 x_serialization_backend: 'YAML::Tiny version 1.73' x_spdx_expression: 'Artistic-1.0-Perl OR GPL-1.0-or-later' x_static_install: 1 MANIFEST100644001750001750 111313712054757 15546 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25# This file was automatically generated by Dist::Zilla::Plugin::Manifest v6.015. Build.PL Changes LICENSE MANIFEST META.json META.yml README cpanfile dist.ini lib/CGI/Compile.pm t/00_compile.t t/Exit.pm t/alarm.t t/args.cgi t/author-pod-syntax.t t/chained_exit.t t/chdir.t t/coderef_args.t t/compile.t t/data.cgi t/data_crlf.cgi t/data_end.t t/end.cgi t/end_crlf.cgi t/error.cgi t/eval-my-variables.t t/exit-code.t t/exit.cgi t/exit.t t/hello.cgi t/local-SIG.t t/psgi.t t/race-conditions.t t/return-val.t t/source.t t/source_filter.t t/switch.cgi t/symbols.t t/warnings.cgi t/warnings.t Exit.pm100644001750001750 4413712054757 16071 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/tpackage Exit; sub main { exit } 1; alarm.t100644001750001750 100713712054757 16143 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/t#!/usr/bin/env perl; use strict; use warnings; use Test::More $^O eq 'MSWin32' ? ( skip_all => 'not supported on Win32') : ( tests => 1 ); use CGI::Compile; use Capture::Tiny 'capture_stdout'; my $cgi =<<'EOL'; #!/usr/bin/perl use strict; use warnings; use Time::HiRes 'ualarm'; print "Content-Type: text/plain\015\012\015\012"; $SIG{ALRM} = sub { print "ALARM\015\012" }; ualarm 50; sleep 1; EOL my $sub = CGI::Compile->compile(\$cgi); like capture_stdout { $sub->() }, qr/ALARM/; done_testing; chdir.t100644001750001750 26513712054757 16125 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/tuse strict; use Test::More; use CGI::Compile; use Cwd; my $sub = CGI::Compile->compile("t/error.cgi"); my $dir = Cwd::cwd; eval { $sub->() }; is Cwd::cwd, $dir; done_testing; end.cgi100755001750001750 16113712054757 16077 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/t#!/usr/bin/perl print "Content-Type: text/plain\015\012\015\012Hello"; __END__ =head1 NAME Hello World =cut META.json100644001750001750 506113712054757 16044 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25{ "abstract" : "Compile .cgi scripts to a code reference like ModPerl::Registry", "author" : [ "Tatsuhiko Miyagawa " ], "dynamic_config" : 0, "generated_by" : "Dist::Milla version v1.0.20, Dist::Zilla version 6.015, CPAN::Meta::Converter version 2.150010", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "CGI-Compile", "no_index" : { "directory" : [ "eg", "examples", "inc", "share", "t", "xt" ] }, "prereqs" : { "configure" : { "requires" : { "Module::Build::Tiny" : "0.034" }, "suggests" : { "JSON::PP" : "2.27300" } }, "develop" : { "requires" : { "CGI::Emulate::PSGI" : "0", "Dist::Milla" : "v1.0.20", "Dist::Zilla" : "0", "Dist::Zilla::PluginBundle::Milla" : "0", "Plack::Test" : "0", "Test::Pod" : "1.41" } }, "runtime" : { "requires" : { "File::pushd" : "0", "Sub::Name" : "0", "perl" : "5.008001" } }, "test" : { "requires" : { "CGI" : "0", "Capture::Tiny" : "0", "Sub::Identify" : "0", "Switch" : "0", "Test::More" : "0", "Test::NoWarnings" : "0", "Test::Requires" : "0", "Try::Tiny" : "0" } } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/miyagawa/CGI-Compile/issues" }, "homepage" : "https://github.com/miyagawa/CGI-Compile", "repository" : { "type" : "git", "url" : "https://github.com/miyagawa/CGI-Compile.git", "web" : "https://github.com/miyagawa/CGI-Compile" } }, "version" : "0.25", "x_contributors" : [ "Hans Dieter Pearcey ", "J\u00f6rn Reder ", "kocoureasy ", "lestrrat ", "Pavel Mateja ", "Rafael Kitover ", "Rafael Kitover ", "Torsten F\u00f6rtsch " ], "x_generated_by_perl" : "v5.30.3", "x_serialization_backend" : "Cpanel::JSON::XS version 4.19", "x_spdx_expression" : "Artistic-1.0-Perl OR GPL-1.0-or-later", "x_static_install" : 1 } args.cgi100644001750001750 15013712054757 16260 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/tprint "Content-Type: text/plain\n\n", "Hello \@_: ", join(',' => @_), ' @ARGV: ', join(',' => @ARGV); data.cgi100755001750001750 14113712054757 16240 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/t#!/usr/bin/perl print "Content-Type: text/plain\015\012\015\012", ; __DATA__ Hello World exit.cgi100644001750001750 11413712054757 16275 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/tuse Exit; print "Content-Type: text/html\015\012\015\012Hello"; Exit::main; source.t100644001750001750 55413712054757 16335 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/t#!/usr/bin/env perl use strict; use warnings; use Test::More; use CGI::Compile; use Capture::Tiny 'capture_stdout'; my $cgi =<<'EOL'; #!/usr/bin/perl print "Content-Type: text/plain\015\012\015\012", ; __DATA__ Hello World EOL my $sub = CGI::Compile->compile(\$cgi); my $out = capture_stdout { $sub->() }; like $out, qr/Hello\nWorld/; done_testing; compile.t100644001750001750 171513712054757 16505 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/tuse Test::More; use CGI; use Capture::Tiny 'capture_stdout'; use CGI::Compile; #no warnings 'signal'; # for MSWin32 my %orig_sig = %SIG; # set something special $SIG{TERM} = $orig_sig{TERM} = sub {TERM}; # perl < 5.8.9 won't set a %SIG entry to undef, it sets it to '' %orig_sig = map { defined $_ ? $_ : '' } %orig_sig if $] < 5.008009; $orig_sig{USR1} = 'IGNORE' if $^O eq 'MSWin32'; my $sub = CGI::Compile->compile("t/hello.cgi"); is_deeply \%SIG, \%orig_sig, '%SIG preserved during compile'; $ENV{REQUEST_METHOD} = 'GET'; $ENV{QUERY_STRING} = 'name=foo'; my $stdout = capture_stdout { $sub->() }; like $stdout, qr/Hello foo counter=1/; is_deeply \%SIG, \%orig_sig, '%SIG preserved during run'; $ENV{QUERY_STRING} = 'exit_status=1'; eval { capture_stdout { $sub->() } }; like $@, qr/^exited nonzero: 1 /, 'non-zero exit status'; $ENV{QUERY_STRING} = 'name=bar'; $stdout = capture_stdout { $sub->() }; like $stdout, qr/Hello bar counter=3/; done_testing; error.cgi100644001750001750 1313712054757 16433 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/tdie "foo"; hello.cgi100644001750001750 50513712054757 16433 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/tuse CGI; $COUNTER++; BEGIN { $SIG{USR1} = 'IGNORE'; $SIG{TERM} = sub {"COMPILE TERM"} } $SIG{USR1} = 'IGNORE'; $SIG{TERM} = sub {"RUN TERM"}; my $q = CGI->new; chomp(my $greeting = ); print $q->header, $greeting, scalar $q->param('name'), " counter=$COUNTER"; exit $q->param('exit_status') || 0; __DATA__ Hello symbols.t100644001750001750 140013712054757 16534 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/t#!/usr/bin/env perl use strict; use warnings; use Test::More; use CGI::Compile; use Capture::Tiny 'capture_stdout'; use Test::Requires 'Sub::Identify'; use Sub::Identify qw/sub_name stash_name/; my $cgi =<<'EOL'; #!/usr/bin/perl print "Content-Type: text/plain\015\012\015\012", ; __DATA__ Hello World EOL my $sub = CGI::Compile->compile(\$cgi); is sub_name($sub), '__CGI0__'; is stash_name($sub), 'main'; $sub = CGI::Compile->compile('t/hello.cgi'); is sub_name($sub), 'hello_2ecgi'; like stash_name($sub), qr/^CGI::Compile::ROOT::[A-Za-z0-9_]*t_hello_2ecgi\z/; $sub = CGI::Compile->compile(\$cgi); is sub_name($sub), '__CGI1__'; is stash_name($sub), 'main'; my $out = capture_stdout { $sub->() }; like $out, qr/Hello\nWorld/; done_testing; data_end.t100644001750001750 173213712054757 16613 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/tuse Test::More; use CGI::Compile; use Capture::Tiny 'capture_stdout'; { my $sub = CGI::Compile->compile("t/data.cgi"); my $out = capture_stdout { $sub->() }; like $out, qr/Hello\nWorld/; } { my $sub = CGI::Compile->compile("t/data_crlf.cgi"); my $out = capture_stdout { $sub->() }; like $out, qr/Hello\r?\nWorld/; } eval { my $sub = CGI::Compile->compile("t/end.cgi"); }; is $@, ''; eval { my $sub = CGI::Compile->compile("t/end_crlf.cgi"); }; is $@, ''; { local $main::FLNO; my $sub = CGI::Compile->compile(\<<'EOF'); $main::FLNO = fileno DATA; print +()[0,3]; __DATA__ line 1 line 2 line 3 line 4 EOF my $out = capture_stdout { $sub->() }; like $out, qr/line 1\r?\nline 4/; $out = capture_stdout { $sub->() }; like $out, qr/line 1\r?\nline 4/; is $main::FLNO, -1; } { local $main::S; my $sub = CGI::Compile->compile(\<<'EOF'); $main::S = $^S; EOF $sub->(); is $main::S, 1; } done_testing; switch.cgi100755001750001750 15713712054757 16637 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/t#!/usr/bin/perl use strict; use warnings; use Switch; switch (42) { case 42 { print "switch works\n" } } warnings.t100644001750001750 40613712054757 16661 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/tuse Test::More; use Capture::Tiny 'capture_stdout'; use CGI::Compile; my $sub = CGI::Compile->compile("t/warnings.cgi"); my $stdout = do { local $^W = 0; capture_stdout { $sub->() }; }; like $stdout, qr/\s*1\z/, '-w switch preserved'; done_testing; exit-code.t100644001750001750 266013712054757 16736 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/t#!perl use Test::More tests => 13 * 4; use CGI::Compile; my $exit_return_val = sub { return CGI::Compile->new(return_exit_val => 1)->compile(shift)->(); }; my $exit_return_val_global = sub { no warnings 'once'; local $CGI::Compile::RETURN_EXIT_VAL = 1; return CGI::Compile->compile(shift)->(); }; my $throw_exit_val = sub { my $rv = eval { CGI::Compile->compile(shift)->() }; ok( (defined($rv) && $rv =~ /^\d+\Z/ && $@ eq '') || (!defined($rv) && $@ =~ /^exited nonzero: (\d+) /) ); $rv = $1 if !defined($rv); return $rv; }; foreach my $method ($exit_return_val, $exit_return_val_global, $throw_exit_val) { is ($method->(\'0;'), 0, 'fall-through exit 0'); is ($method->(\'exit 0;'), 0, 'function exit 0'); is ($method->(\'1;'), 1, 'fall-through exit 1'); is ($method->(\'2.6;'), 3, 'fall-through float rounded up to int'); is ($method->(\'4.4;'), 4, 'fall-through float rounded down to int'); is ($method->(\'exit 1;'), 1, 'function exit 1'); is ($method->(\'"blah";'), 0, 'fall-through exit string'); is ($method->(\'exit "blah";'), 0, 'function exit string'); is ($method->(\'"";'), 0, 'fall-through exit empty string'); is ($method->(\'exit "";'), 0, 'function exit empty string'); is ($method->(\';'), 0, 'fall-through exit undef'); is ($method->(\'exit;'), 0, 'function exit implicit undef'); is ($method->(\'exit undef;'), 0, 'function exit explicit undef'); } local-SIG.t100644001750001750 74313712054757 16547 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/t#!perl use Capture::Tiny 'capture_stdout'; use CGI::Compile; use POSIX qw(:signal_h); use Test::More $^O eq 'MSWin32' ? ( skip_all => 'not supported on Win32') : ( tests => 1 ); unless (defined sigprocmask(SIG_UNBLOCK, POSIX::SigSet->new(SIGQUIT))) { die "Could not unblock SIGQUIT\n"; } my $sub = CGI::Compile->compile(\<<'EOF'); $SIG{QUIT} = sub{print "QUIT\n"}; kill QUIT => $$; print "END\n"; EOF is capture_stdout { $sub->() }, "QUIT\nEND\n", 'caught signal'; 00_compile.t100644001750001750 11013712054757 16750 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/tuse strict; use Test::More tests => 1; BEGIN { use_ok 'CGI::Compile' } end_crlf.cgi100755001750001750 17413712054757 17111 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/t#!/usr/bin/perl print "Content-Type: text/plain\015\012\015\012Hello"; __END__ =head1 NAME Hello World =cut return-val.t100644001750001750 154513712054757 17155 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/t#!perl use strict; use warnings; use Test::More 'no_plan'; use Test::NoWarnings; use CGI::Compile; my $SHEBANG = "#!perl -w\n"; my %VALS = ('undef' => 0, '"bla"' => 0); my %NUM_VALS = (0.5 => 0, 1.2 => 1, 2.7 => 3, 0 => 0, 1240 => 216); my %TESTS; my $gen_keys = sub { my @r; foreach my $cmd ('', 'return ', 'exit ') { push @r, $SHEBANG . "${cmd}$_[0];\n"; } @r; }; while (my ($k, $v) = each %VALS) { $TESTS{$_} = $v foreach $gen_keys->($k); } while (my ($k, $v) = each %NUM_VALS) { $TESTS{$_} = $v foreach $gen_keys->($k); $TESTS{$_} = $v foreach $gen_keys->(qq|"${k}bla"|); } while (my ($k, $v) = each %TESTS) { local $@; eval { is(CGI::Compile->compile(\$k)->(), $v, 'return val from CGI'); }; if ($@ && $@ =~ /^exited nonzero: (\d+) /) { is($1, $v, 'nonzero exit val from CGI'); } } warnings.cgi100755001750001750 11113712054757 17154 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/t#!/usr/bin/perl -w print "Content-Type: text/plain\015\012\015\012$^W"; data_crlf.cgi100755001750001750 15013712054757 17246 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/t#!/usr/bin/perl print "Content-Type: text/plain\015\012\015\012", ; __DATA__ Hello World chained_exit.t100644001750001750 36413712054757 17460 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/tuse strict; use warnings; use Test::More tests => 2; use Test::NoWarnings; my $exit_chained; sub CORE::GLOBAL::exit (;$) { $exit_chained = 1; } use CGI::Compile; eval "exit"; is $exit_chained, 1, 'exit chained to CORE::GLOBAL::exit'; coderef_args.t100644001750001750 35113712054757 17453 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/tuse Test::More; use Capture::Tiny 'capture_stdout'; use CGI::Compile; my $sub = CGI::Compile->compile("t/args.cgi"); my $stdout = capture_stdout { $sub->(1,2,3) }; like $stdout, qr/Hello \@_: 1,2,3 \@ARGV: 1,2,3\z/; done_testing; source_filter.t100644001750001750 53513712054757 17701 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/tuse Test::More; use Switch; use Capture::Tiny 'capture_stdout'; use CGI::Compile; my $sub = eval { CGI::Compile->compile("t/switch.cgi"); }; if ($@) { fail 'CGI with source filter compiles'; done_testing; exit; } my $stdout = capture_stdout { $sub->() }; is $stdout, "switch works\n", 'source filter works in CGI'; done_testing; CGI000755001750001750 013712054757 15431 5ustar00rkitoverrkitover000000000000CGI-Compile-0.25/libCompile.pm100644001750001750 3306413712054757 17545 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/lib/CGIpackage CGI::Compile; use strict; use 5.008_001; our $VERSION = '0.25'; use Cwd; use File::Basename; use File::Spec::Functions; use File::pushd; use File::Temp; use File::Spec; use File::Path; use Sub::Name 'subname'; our $RETURN_EXIT_VAL = undef; sub new { my ($class, %opts) = @_; $opts{namespace_root} ||= 'CGI::Compile::ROOT'; bless \%opts, $class; } our $USE_REAL_EXIT; BEGIN { $USE_REAL_EXIT = 1; my $orig = *CORE::GLOBAL::exit{CODE}; my $proto = $orig ? prototype $orig : prototype 'CORE::exit'; $proto = $proto ? "($proto)" : ''; $orig ||= sub { my $exit_code = shift; CORE::exit(defined $exit_code ? $exit_code : 0); }; no warnings 'redefine'; *CORE::GLOBAL::exit = eval qq{ sub $proto { my \$exit_code = shift; \$orig->(\$exit_code) if \$USE_REAL_EXIT; die [ "EXIT\n", \$exit_code || 0 ] }; }; die $@ if $@; } my %anon; sub compile { my($class, $script, $package) = @_; my $self = ref $class ? $class : $class->new; my($code, $path, $dir, $subname); if (ref $script eq 'SCALAR') { $code = $$script; $package ||= (caller)[0]; $subname = '__CGI' . $anon{$package}++ . '__'; } else { $code = $self->_read_source($script); $path = Cwd::abs_path($script); $dir = File::Basename::dirname($path); my $genned_package; ($genned_package, $subname) = $self->_build_subname($path || $script); $package ||= $genned_package; } my $warnings = $code =~ /^#!.*\s-w\b/ ? 1 : 0; $code =~ s/^__END__\r?\n.*//ms; $code =~ s/^__DATA__\r?\n(.*)//ms; my $data = defined $1 ? $1 : ''; # TODO handle nph and command line switches? my $eval = join '', "package $package;", 'sub {', 'local $CGI::Compile::USE_REAL_EXIT = 0;', "\nCGI::initialize_globals() if defined &CGI::initialize_globals;", 'local ($0, $CGI::Compile::_dir, *DATA);', '{ my ($data, $path, $dir) = @_[1..3];', ($path ? '$0 = $path;' : ''), ($dir ? '$CGI::Compile::_dir = File::pushd::pushd $dir;' : ''), q{open DATA, '<', \$data;}, '}', # NOTE: this is a workaround to fix a problem in Perl 5.10 q(local @SIG{keys %SIG} = do { no warnings 'uninitialized'; @{[]} = values %SIG };), "local \$^W = $warnings;", 'my $rv = eval {', 'local @ARGV = @{ $_[4] };', # args to @ARGV 'local @_ = @{ $_[4] };', # args to @_ as well ($path ? "\n#line 1 $path\n" : ''), $code, "\n};", q{ { no warnings qw(uninitialized numeric pack); my $self = shift; my $exit_val = unpack('C', pack('C', sprintf('%.0f', $rv))); if ($@) { die $@ unless ( ref($@) eq 'ARRAY' and $@->[0] eq "EXIT\n" ); my $exit_param = unpack('C', pack('C', sprintf('%.0f', $@->[1]))); if ($exit_param != 0 && !$CGI::Compile::RETURN_EXIT_VAL && !$self->{return_exit_val}) { die "exited nonzero: $exit_param"; } $exit_val = $exit_param; } return $exit_val; } }, '};'; my $sub = do { no warnings 'uninitialized'; # for 5.8 # NOTE: this is a workaround to fix a problem in Perl 5.10 local @SIG{keys %SIG} = @{[]} = values %SIG; local $USE_REAL_EXIT = 0; my $code = $self->_eval($eval); my $exception = $@; die "Could not compile $script: $exception" if $exception; subname "${package}::$subname", sub { my @args = @_; # this is necessary for MSWin32 my $orig_warn = $SIG{__WARN__} || sub { warn(@_) }; local $SIG{__WARN__} = sub { $orig_warn->(@_) unless $_[0] =~ /^No such signal/ }; $code->($self, $data, $path, $dir, \@args) }; }; return $sub; } sub _read_source { my($self, $file) = @_; open my $fh, "<", $file or die "$file: $!"; return do { local $/; <$fh> }; } sub _build_subname { my($self, $path) = @_; my ($volume, $dirs, $file) = File::Spec::Functions::splitpath($path); my @dirs = File::Spec::Functions::splitdir($dirs); my $name = $file; my $package = join '_', grep { defined && length } $volume, @dirs, $name; # Escape everything into valid perl identifiers s/([^A-Za-z0-9_])/sprintf("_%2x", unpack("C", $1))/eg for $package, $name; # make sure the identifiers don't start with a digit s/^(\d)/_$1/ for $package, $name; $package = $self->{namespace_root} . ($package ? "::$package" : ''); return ($package, $name); } # define tmp_dir value later on first usage, otherwise all children # share the same directory when forked my $tmp_dir; sub _eval { my $code = \$_[1]; # we use a tmpdir chmodded to 0700 so that the tempfiles are secure $tmp_dir ||= File::Spec->catfile(File::Spec->tmpdir, "cgi_compile_$$"); if (! -d $tmp_dir) { mkdir $tmp_dir or die "Could not mkdir $tmp_dir: $!"; chmod 0700, $tmp_dir or die "Could not chmod 0700 $tmp_dir: $!"; } my ($fh, $fname) = File::Temp::tempfile('cgi_compile_XXXXX', UNLINK => 1, SUFFIX => '.pm', DIR => $tmp_dir); print $fh $$code; close $fh; my $sub = do $fname; unlink $fname or die "Could not delete $fname: $!"; return $sub; } END { if ($tmp_dir and -d $tmp_dir) { File::Path::remove_tree($tmp_dir); } } 1; __END__ =encoding utf-8 =for stopwords =head1 NAME CGI::Compile - Compile .cgi scripts to a code reference like ModPerl::Registry =head1 SYNOPSIS use CGI::Compile; my $sub = CGI::Compile->compile("/path/to/script.cgi"); =head1 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. B for best results, load L before any modules used by your CGIs. =head1 RUN ON PSGI Combined with L, 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 =head1 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 C. To solve this, you have to update your code to pass around the lexical variables, or replace C with C. See also L for more details. =head1 METHODS =head2 new Does not need to be called, you only need to call it if you want to set your own C for the generated packages into which the CGIs are compiled into. Otherwise you can just call L as a class method and the object will be instantiated with a C of C. You can also set C, see L for details. Example: my $compiler = CGI::Compile->new(namespace_root => 'My::CGIs'); my $cgi = $compiler->compile('/var/www/cgi-bin/my.cgi'); =head2 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 L above. Parameters: =over 4 =item * C<$cgi_script> Path to perl CGI script file or a scalar reference that contains the source code of CGI script, required. =item * C<$package> Optional, package to install the script into, defaults to the path parts of the script joined with C<_>, and all special characters converted to C<_%2x>, prepended with C. E.g.: /var/www/cgi-bin/foo.cgi becomes: CGI::Compile::ROOT::var_www_cgi_2dbin_foo_2ecgi =back Returns: =over 4 =item * C<$coderef> C<$cgi_script> or C<$$code> compiled to coderef. =back =head1 SCRIPT ENVIRONMENT =head2 ARGUMENTS Things like the query string and form data should generally be in the appropriate environment variables that things like L expect. You can also pass arguments to the generated coderef, they will be locally aliased to C<@_> and C<@ARGV>. =head2 C and C blocks C blocks are called once when the script is compiled. C 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. C blocks will be called once for each worker process and another time for the parent process while C blocks are called only by the parent process. =head2 C<%SIG> The C<%SIG> hash is preserved meaning the script can change signal handlers at will. The next invocation gets a pristine C<%SIG> again. =head2 C and exceptions Calls to C are intercepted and converted into exceptions. When the script calls C and exception is thrown and C<$@> contains a reference pointing to the array ["EXIT\n", 19] Naturally, L (exceptions being caught) is always C during script runtime. If you really want to exit the process call C or set C<$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. =head2 Return Code The generated coderef's exit value is either the parameter that was passed to C or the value of the last statement of the script. The return code is converted into an integer. On a C<0> exit, the coderef will return C<0>. On an explicit non-zero exit, by default an exception will be thrown of the form: exited nonzero: where C is the exit value. This only happens for an actual call to L, 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 L. Alternately, you can change this behavior globally by setting: $CGI::Compile::RETURN_EXIT_VAL = 1; =head2 Current Working Directory If C<< 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. =head2 C and C These file handles are not touched by C. =head2 The C file handle If the script reads from the C file handle, it reads the C<__DATA__> section provided by the script just as a normal script would do. Note, however, that the file handle is a memory handle. So, C will return C<-1>. =head2 CGI.pm integration If the subroutine C is defined at script runtime, it is called first thing by the compiled coderef. =head1 PROTECTED METHODS These methods define some of the internal functionality of L and may be overloaded if you need to subclass this module. =head2 _read_source Reads the source of a CGI script. Parameters: =over 4 =item * C<$file_path> Path to the file the contents of which is to be read. =back Returns: =over 4 =item * C<$source> The contents of the file as a scalar string. =back =head2 _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 C<$self->{namespace_root}>. Parameters: =over 4 =item * C<$file_path> The path to the CGI script file, the package name is generated based on this path. =back Returns: =over 4 =item * C<$package> The generated package name. =back =over 4 =item * C<$subname> The generated coderef name, based on the file name (without directory) of the CGI file path. =back =head2 _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 L so that there are no issues with lexical context or source filters. Parameters: =over 4 =item * C<$code> The generated code that will make the coderef for the CGI. =back Returns: =over 4 =item * C<$coderef> The coderef that is the resulting of evaluating the generated perl code. =back =head1 AUTHOR Tatsuhiko Miyagawa Emiyagawa@bulknews.netE =head1 CONTRIBUTORS Rafael Kitover Erkitover@gmail.comE Hans Dieter Pearcey Ehdp@cpan.orgE kocoureasy Eigor.bujna@post.czE Torsten Förtsch Etorsten.foertsch@gmx.netE Jörn Reder Ejreder@dimedis.deE Pavel Mateja Epavel@verotel.czE lestrrat Elestrrat+github@gmail.comE =head1 COPYRIGHT & LICENSE Copyright (c) 2020 Tatsuhiko Miyagawa Emiyagawa@bulknews.netE This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO L L =cut race-conditions.t100644001750001750 305213712054757 20132 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/t#!perl use strict; use warnings; use Test::More $^O eq 'MSWin32' ? ( skip_all => 'no fork() on Win32') : ( tests => $ENV{AUTOMATED_TESTING} ? 3600 : 75 ); use CGI; use CGI::Compile; use POSIX ':sys_wait_h'; use Capture::Tiny qw/capture_stdout capture_stderr/; use Try::Tiny; my %children; $SIG{CHLD} = sub { while ((my $child = waitpid(-1, WNOHANG)) > 0) { delete $children{$child}; ok($? >> 8 == 0, "no race condition in child PID=$child"); } }; # 400 iterations when smoking, 25 otherwise. for (1..($ENV{AUTOMATED_TESTING} ? 400 : 25)) { my $errors = capture_stderr { # Use 8 simultaneous processes when smoking, 2 otherwise. for (1..($ENV{AUTOMATED_TESTING} ? 8 : 2)) { defined(my $child = fork()) or die "fork() failed: $!"; if ($child == 0) { # child try { my $sub = CGI::Compile->compile("t/hello.cgi"); $ENV{REQUEST_METHOD} = 'GET'; $ENV{QUERY_STRING} = 'name=foo'; capture_stdout { $sub->() } =~ /^Hello foo/m or exit(1); } catch { print STDERR $_; exit 1; }; exit 0; } else { # parent $children{$child} = 1; } } # Wait for SIGCHLD reaper. select(undef, undef, undef, 0.005) while keys %children; }; is $errors, '', 'no errors during compilation, runtime or global destruction'; } done_testing; eval-my-variables.t100644001750001750 60613712054757 20353 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/t#!perl use Test::More tests => 2 * 6; use CGI::Compile; for my $var (qw/self VERSION data path dir self/) { my $sub = eval { my $script = 'use strict; $'.$var; CGI::Compile->compile(\$script); }; my $exc = 'Global symbol "\$'.$var.'" requires explicit package name'; like $@, qr/$exc/, 'exception '.$var; is $sub, undef, 'compilation failed '.$var; } author-pod-syntax.t100644001750001750 45413712054757 20442 0ustar00rkitoverrkitover000000000000CGI-Compile-0.25/t#!perl BEGIN { unless ($ENV{AUTHOR_TESTING}) { print qq{1..0 # SKIP these tests are for testing by the author\n}; exit } } # This file was automatically generated by Dist::Zilla::Plugin::PodSyntaxTests. use strict; use warnings; use Test::More; use Test::Pod 1.41; all_pod_files_ok();