【问题标题】:How to access variable of other perl program in my perl program如何在我的 perl 程序中访问其他 perl 程序的变量
【发布时间】:2014-04-30 00:06:09
【问题描述】:

我有三个不同的 Perl 程序。我想访问其他两个 Perl 程序中第一个程序中存在的变量的值。

我的第一个 Perl 程序如下所示:

#!/usr/bin/perl
print "Content-Type: text/html; charset=utf-8\n\n";

use CGI;
use Cwd;
use utf8;
$q=new CGI;
$a=$q->param('file');
#chomp($a);
my $ftpname="$a";

此程序从 HTML 程序的文本中获取值。我需要其他 Perl 程序中的 $ftpname 变量的值。我该怎么做?

【问题讨论】:

  • 还有哪两个 Perl 程序?它们与这个有什么关系? (为什么不使用strictwarnings?)
  • 这是错误的做事方式; Perl 有模块,模块通常使用package 语句为自己创建一个命名空间。并且脚本通过userequire 调用这些模块的使用,此时模块的包变量变为可用。在大多数情况下访问模块的包变量仍然是一种不好的形式,但它比在某些不创建自己的命名空间的库上使用 do filename.pl 更好。
  • @Quentin 我认为其他程序的外观并不重要,他在问如何从另一个程序中读取一个程序中定义的变量的值。
  • @user3395299 我不认为你可以直接在程序之间进行交互,除非一个程序调用另一个程序。例如,如果该程序返回 $ftpname 的值,则在其他程序中,您可以将该程序用作函数,并将其结果存储在变量中。另一种方法是将 $ftpname 存储在临时文件中,并让其他程序读取该临时文件。
  • @AdiInbar — 这使得他们互动的方式非常相关。他们会向其他 CGI 程序发出 HTTP 请求吗?脱壳?解析源代码。

标签: html perl cgi


【解决方案1】:

您的问题的答案最终是您需要使用会话将状态信息从一页保存到下一页。 CGI::Session::Tutorial 非常适合介绍这个概念和一些早期的替代方案。

您将在下面找到演示这种做法的两个脚本。但首先。

  1. 始终在您制作的每个 perl 脚本的顶部包含 use strict;use warnings;。这将使您成为更好的编码人员并帮助您更快地发现错误。更多原因请查看:Why use strict and warnings?

  2. 因为你使用的是CGI,你也应该使用CGI::Carp。只需在 use CGI; 语句后添加以下行:use CGI::Carp qw(fatalsToBrowser);

  3. 您的问题将通过使用CGI::Session 在页面之间保存状态信息来解决。以下脚本将变量保存在第二个脚本使用的第一个脚本中。您应该能够根据您的 3 脚本场景进行调整。

  4. 您最好先输出一个 http 标头,但让CGI 为您做这件事。通常,我会说使用print $q->header();。这将输出与您手动执行的相同的标题。但是,因为我们希望 CGI::Session 能够设置 cookie,所以我将演示 print $session->header(); 的用法。

第一个名为step1.pl 的脚本设置了一个随机变量并将其保存到会话中。设置完成后,它会提供指向下一步的链接。

#!/usr/bin/perl

use strict;
use warnings;

use CGI;
use CGI::Carp qw(fatalsToBrowser);
use CGI::Session;

my $q = CGI->new;
my $session = CGI::Session->new($q) or die CGI->Session->errstr;
print $session->header();

$session->save_param($q, ['dependentvar']);
#$session->load_param($q, ['dependentvar']); # Uncomment this if you want the text field initialized when returning to the form.

# Random Page View Count
my $count = 1 + ($session->param('count') // 0);
$session->param('count' => $count);

print qq{
<html>
<head><title>Step 1</title></head>
<body>
<h3>Step 1</h3>
<p>Goal: Set a random value that must be initialized before proceeding to step 2.</p>
<p>Number of Page views this session: $count</p>
<div><form method="POST">};

print $q->textfield("dependentvar");
print $q->submit("set");

print qq{</form></div>};

if ($q->param('dependentvar')) {
    print qq{<div>You have a value set, you can proceed to <a href="step2.pl">step 2</a></div>};
}

print qq{
</body>
</html>};

还有第二个脚本step2.pl,它要求在继续之前设置dependentvar,然后让用户对字符串执行一些翻译。

#!/usr/bin/perl

use strict;
use warnings;

use CGI;
use CGI::Carp qw(fatalsToBrowser);
use CGI::Session;

my $q = CGI->new;
my $session = CGI::Session->new($q) or die CGI->Session->errstr;
print $session->header();

my $var = $session->param('dependentvar');

# Verify that our dependent variable was set in the previous step.
if (!$var) {
    print qq{
<html>
<head><title>Step 2</title></head>
<body>
<h3>Step 2</h3>
<p>You must set a value in <a href="step1.pl">step 1</a> before proceeding with step 2</p>
</body>
<html>};
    exit;
}

print qq{
<html>
<head><title>Step 2</title></head>
<body>
<h3>Step 2</h3>
<p>Goal: Take variable from <a href="step1.pl">Step 1</a> and perform some translations on it.</p>
<div><form method="POST">};

print q{<div>} . $q->checkbox('double') . q{</div>};
print q{<div>} . $q->checkbox('reverse') . q{</div>};
print q{<div>} . $q->checkbox('upper') . q{</div>};
print q{<div>} . $q->checkbox('lower') . q{</div>};
print q{<div>} . $q->checkbox('ucfirst') . q{</div>};
print q{<div>} . $q->checkbox('lcfirst') . q{</div>};

print q{<p>} . $q->submit("transform") . q{</p>};

print qq{</form></div>};

if ($q->param('transform')) {
    my $newvar = $var;
    $newvar x= 2 if $q->param('double');
    $newvar = reverse $newvar if $q->param('reverse');
    $newvar = uc $newvar if $q->param('upper');
    $newvar = lc $newvar if $q->param('lower');
    $newvar = ucfirst $newvar if $q->param('ucfirst');
    $newvar = lcfirst $newvar if $q->param('lcfirst');

    print qq{<div>$var -&gt; $newvar</div>};
}

print qq{
</body>
</html>};

这应该作为对会话概念的介绍。还有其他方法可以将变量从一个 Web 脚本传递到下一个,但正如在 CGI::Session::Tutorial 中讨论的那样,这种方法非常脆弱。最后,请在将此解决方案应用于您的代码之前阅读整个教程。

【讨论】:

    【解决方案2】:

    解决问题的方法可能不止一种。

    1. 您可以将$a 的值保存在一个文本文件中,然后在其他两个程序中读取该文本文件。
    2. 您可以创建一个模块,其中包含作用于$a 值的函数。然后您可以使用 $a 作为参数调用正确的 moduleName::functionName。一个简单的入门教程是here
    3. 如果你想共享变量,你可以有一个模块作为带有变量声明的某种头文件(使用our关键字和/或导出),然后这个模块是used在所有其他 perl 脚本中,该变量随处可见。见this answer。有关变量范围的更多帮助,您可以阅读此参考:Coping with Scoping

    【讨论】:

    猜你喜欢
    • 2011-10-21
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 2010-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多