在mod_perl环境中调用清理代码

发布于 2024-07-25 10:58:18 字数 683 浏览 5 评论 0原文

一些引用来自实用的 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 } );

但是,

  1. 如何在a.cgi脚本中获取$r?
  2. 子例程可以访问 my 范围标志变量吗?
  3. 这个 $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,

  1. How I can obtain $r in a.cgi script?
  2. Can the subroutine access the my scope flag variable?
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静水深流 2024-08-01 10:58:18
  1. 我的$r = Apache->请求;

  2. 是的,但请参阅http://modperlbook.org /html/6-2-Exusing-Apache-Registry-Secrets.html 和接下来的几页,有关局部变量和函数的范围。

  3. 是的,仅在您希望运行时才注册该函数。

  1. my $r = Apache->request;

  2. 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.

  3. Yes, only register the function if you want it to run.

失而复得 2024-08-01 10:58:18

如果我理解正确的话,您有一个脚本想要在 mod_perl 下运行并作为普通 CGI 运行,听起来您正在使用 Apache::Registry 来执行此操作。

您有清理代码,当您作为 CGI 脚本运行时才运行。

您需要检测您是否在 mod_perl 下运行。 这相当容易。 最简单的方法是检查您的环境:

unless ($ENV{MOD_PERL})
{
   #... cleanup code here.
}

如果您希望在脚本在 Apache::Registry 下终止时运行某些内容,则只需注册一个清理处理程序。

如果您确实想要这样做,您应该将清理代码放入子程序中,并从 CGI 中的检查

unless ($ENV{MOD_PERL})
{
   cleanup_sub();
}

和清理处理程序中调用该子程序:

my $r = Apache->request;
$r->register_cleanup(sub { cleanup_sub() } );

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:

unless ($ENV{MOD_PERL})
{
   #... cleanup code here.
}

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:

unless ($ENV{MOD_PERL})
{
   cleanup_sub();
}

and from your cleanup handler:

my $r = Apache->request;
$r->register_cleanup(sub { cleanup_sub() } );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文