在mod_perl环境中调用清理代码
一些引用来自实用的 mod_perl
“通常,单个进程在退出之前会处理许多请求,因此如果希望 END 块在每个请求处理结束时执行某些操作,则不能使用 END 块。”
,在我的 a.cgi 脚本中:
my $flag = 1;
END {
# Value for $flag is undefined, if this script is run under mod_perl.
# END block code only executed when process to handle a.cgi exit.
# I wish to execute some code, just before process to handle a.cgi exit.
if ($flag) {
# clean up code.
}
}
本书重新开始 $r->register_cleanup(sub { #cleanup } );
但是,
- 如何在a.cgi脚本中获取$r?
- 子例程可以访问 my 范围标志变量吗?
- 这个 $r->register_cleanup 应该放在 a.cgi 脚本中吗? 我只想为 a.cgi 脚本执行清理代码。 其余的则不然。
Some quote to pick from practical mod_perl
"Usually, a single process serves many requests before it exits, so END blocks cannot be used if they are expected to do something at the end of each request's processing."
So, in my a.cgi script :
my $flag = 1;
END {
# Value for $flag is undefined, if this script is run under mod_perl.
# END block code only executed when process to handle a.cgi exit.
# I wish to execute some code, just before process to handle a.cgi exit.
if ($flag) {
# clean up code.
}
}
The book recommences $r->register_cleanup(sub { #cleanup } );
However,
- How I can obtain $r in a.cgi script?
- Can the subroutine access the my scope flag variable?
- Is this $r->register_cleanup shall be placed at a.cgi script? I only want the cleanup code to be executed for a.cgi script. Not the rest.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的$r = Apache->请求;
是的,但请参阅http://modperlbook.org /html/6-2-Exusing-Apache-Registry-Secrets.html 和接下来的几页,有关局部变量和函数的范围。
是的,仅在您希望运行时才注册该函数。
my $r = Apache->request;
Yes, but see http://modperlbook.org/html/6-2-Exposing-Apache-Registry-Secrets.html and the next couple of pages, regarding scoping of local variables and functions.
Yes, only register the function if you want it to run.
如果我理解正确的话,您有一个脚本想要在 mod_perl 下运行并作为普通 CGI 运行,听起来您正在使用 Apache::Registry 来执行此操作。
您有清理代码,仅当您作为 CGI 脚本运行时才运行。
您需要检测您是否在 mod_perl 下运行。 这相当容易。 最简单的方法是检查您的环境:
如果您希望在脚本在 Apache::Registry 下终止时运行某些内容,则只需注册一个清理处理程序。
如果您确实想要这样做,您应该将清理代码放入子程序中,并从 CGI 中的检查
和清理处理程序中调用该子程序:
If I understand this correctly, you have a script you want to run both under mod_perl and as a plain CGI and it sounds like you are using Apache::Registry to do this.
You have cleanup code that you want run only when you are running as CGI script.
You need to detect whether or not you are running under mod_perl. That's fairly easy. The simplest way is to check your environment:
You only to register a cleanup handler if you want something to run when your script terminates under Apache::Registry.
If you do want that, you should place your cleanup code into a sub and call that sub from your check in the CGI:
and from your cleanup handler: