【问题标题】:How can I create a new CGI script programmatically and run it immediately?如何以编程方式创建新的 CGI 脚本并立即运行?
【发布时间】:2010-01-06 00:09:54
【问题描述】:

我编写了一个 Perl 脚本来读取配置文件并创建 CGI 脚本。这工作正常,我在终端上获得了 CGI 脚本的输出,我可以在网页上执行该 CGI 脚本。下面是我的示例脚本。

#!/usr/bin/perl -w

use strict;
use Text::Template;

my $conf = "test.cfg";

open CFG, $conf or die "Could not open config file";
my @rawConfig = <CFG>;
my $config = eval "{".join("",@rawConfig)."}";

my $template = Text::Template->new(TYPE => 'FILE',  SOURCE => 'test.cgi.tmpl');
my $result = $template->fill_in(HASH => $config);
print $result;

通过使用它,我必须保存每个 CGI 并单独执行。我需要帮助来修改此代码,以便我可以直接在 Web 上执行此脚本并在网页上显示输出。

【问题讨论】:

    标签: perl cgi


    【解决方案1】:

    这是一件非常非常可怕的事情,我通常建议不要做任何会创建新程序并让世界在没有人的情况下运行它们的事情。

    不过,我怀疑您可能会这样做,因为您正在创建的 CGI 脚本只需要不同的配置数据,而您已将其存储在脚本本身中。这只是一个猜测,但它是这类事情的通常动机。答案是不在脚本中存储配置数据。我在Mastering Perl 有一整章关于不在代码中存储配置。

    我会非常努力地避免你所描述的情况。

    (Does SO have a spoiler tag?)

    如果您确实需要这样做是有充分理由的(而且几乎从来没有发生过),您可以从原始进程创建文件,然后发出内部重定向到新的 CGI 脚本。你如何做到这一点是你的家庭作业,因为我不会为你把子弹放在枪里。

    【讨论】:

      【解决方案2】:

      多个基于模板的脚本(几乎)永远不是正确答案。

      使用配置文件中的数据结构和控制结构来获得您需要的行为。

      而不是使用模板来创建类似的代码:

      sub foo {
          my $thing = shift;
      
          return blarg( $thing ) * feemb( $thing );
      }
      
      sub bar {
          my $thing = shift;
      
          return crag( $thing ) * forg( $thing );
      }
      
      sub baz {
          my $thing = shift;
      
          return chomb( $thing ) * veezle( $thing );
      }
      

      这样做:

      # Get this from a config file.  YAML perhaps
      my $cfg = {
          foo => [ \&blarg, \&feemb  ],
          bar => [ \&crag,  \&forg   ],
          baz => [ \&chomb, \&veezle ],
      };
      
      sub calculate_product {
          my $cfg   = shift;
          my $type  = shift;
          my $thing = shift;
      
          my @subs_to_call = @{ $cfg->{$type} || [] };
      
          my $result = {shift @subs_to_call}->($thing};
      
          $result *= $_->($thing) for @subs_to_call;
      
          return $result;
      }
      # Call like so:
      my $foo_prod = calculate_product($cfg, 'foo', 15);
      

      您可以通过使用配置信息生成闭包来将配置信息绑定到子例程(即'curry your functions'):

      # Get this from a config file.  YAML perhaps
      my $cfg = {
          foo => [ \&blarg, \&feemb  ],
          bar => [ \&crag,  \&forg   ],
          baz => [ \&chomb, \&veezle ],
      };
      
      my %calculate_product;
      for my $product ( keys %$cfg ) {
      
          my @stored_subs_to_call = @{$cfg->{$product} || [] };
      
          $calculate_product{$prod} = sub {
              my $thing = shift;
      
              my @subs_to_call = @stored_subs_to_call;
      
              my $result = {shift @subs_to_call}->($thing};
      
              $result *= $_->($thing) for @subs_to_call;
      
              return $result;
          }
      }
      
      # Call generated code like so:
      my $foo_prod = $calculate_product{foo}->(15);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-11
        • 1970-01-01
        • 2017-02-03
        • 2011-04-28
        • 1970-01-01
        • 1970-01-01
        • 2013-12-31
        • 1970-01-01
        相关资源
        最近更新 更多