如何在我的Perl程序中访问其他perl程序的变量 [英] How to access variable of other perl program in my perl program

查看:118
本文介绍了如何在我的Perl程序中访问其他perl程序的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

 #!/ usr / bin / perl 
printContent-Type:text / html; charset = utf-8\\\
\\ \
;

使用CGI;
使用Cwd;
使用utf8;
$ q =新的CGI;
$ a = $ q-> param('file');
#chomp($ a);
my $ ftpname =$ a;

该程序从HTML程序的文本中获取价值。我需要Perl程序中另一个$ ftpname变量的值。如何才能做到这一点?

解决方案

您的问题的答案最终是您需要使用会话来保存一个状态信息页到下一页。 CGI :: Session :: Tutorial 是一个很好的概念和一些早期替代品的介绍。



下面你会发现两个脚本来演示这种做法。但首先。 总是包含 使用strict; 在每个perl脚本的顶部使用警告; 。这将使您成为更好的编码器,并帮助您更快找到错误。更多理由请查看: 为什么使用strict和warnings?


  • 因为您使用 CGI ,您还应该使用 CGI :: Carp 。在使用CGI; 语句:使用CGI :: Carp qw(fatalsToBrowser);


  • 您的问题将通过使用 CGI :: Session 来保存页面之间的状态信息。下面的脚本在第二个脚本使用的第一个脚本中保存一个变量。你应该能够适应你的脚本情况。

  • 你最好输出一个http标头,但让 CGI 为你做。通常,我会说使用 print $ q-> header(); 。这将输出与手动操作相同的标题。但是,因为我们希望 CGI :: Session 能够设置cookie,所以我将演示如何使用 print $ session->头文件(); 代替。


    第一个脚本名为 step1.pl 设置一个随机变量并将其保存到会话中。

     #!/ usr / bin / perl 
    $ b一旦它被设置,它就会提供下一步的链接。 $ b使用strict;
    使用警告;

    使用CGI;
    使用CGI :: Carp qw(fatalsToBrowser);
    使用CGI :: Session;

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

    $ session-> save_param($ q,['dependentvar']);
    #$ session-> load_param($ q,['dependentvar']); #如果您希望在返回表单时初始化文本字段,请取消注释。

    #随机页面查看计数
    my $ count = 1 +($ session-> param('count')// 0);
    $ session-> param('count'=> $ count);

    print qq {
    < html>
    < head>< title>第1步< / title>< / head>
    < body>
    < h3>步骤1< / h3>

    目标:设置一个必须初始化的随机值,然后继续执行第2步。< / p>
    < p>本次会话的网页浏览次数:$ 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>您设置了一个值,您可以继续阅读< a href = step2.pl>第2步< / a>< / div>};
    }

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

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

     #!/ usr / bin / perl 

    使用strict;
    使用警告;

    使用CGI;
    使用CGI :: Carp qw(fatalsToBrowser);
    使用CGI :: Session;

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

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

    #确认我们的因变量是在上一步中设置的。
    if(!$ var){
    print qq {
    < html>
    < head>< title>第2步< / title>< / head>
    < body>
    < h3>步骤2< / h3>
    < p>您必须在< a href =step1.pl>步骤1中设定一个值< / a>在继续进行步骤2之前< / p>
    < / body>
    < html>};
    出口;
    }

    print qq {
    < html>
    < head>< title>第2步< / title>< / head>
    < body>
    < h3>步骤2< / h3>
    < p>目标:从< a href =step1.pl>采取变量步骤1< / a>并对其进行翻译。< / p>
    < div>< form method =POST>};

    print q {< div>}。 $ q->复选框('double')。 q {< / DIV>};
    print q {< div>}。 $ q->复选框('reverse')。 q {< / DIV>};
    print q {< div>}。 $ q->复选框('上')。 q {< / DIV>};
    print q {< div>}。 $ q->复选框('lower')。 q {< / DIV>};
    print q {< div>}。 $ q->复选框('ucfirst')。 q {< / DIV>};
    print q {< div>}。 $ q->复选框('lcfirst')。 q {< / DIV>};

    print q {< p>}。 $ q->提交(变换)。 q {< / p为H.};

    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 这种方法非常脆弱。最后,请在将此解决方案应用于您的代码之前阅读完整教程。


    I have three different Perl programs. I want to access the value of a variable present in the first program in the other two Perl programs.

    My first Perl program look like the following:

    #!/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";
    

    This program takes value from a text of HTML program. I need the value of the $ftpname variable in my other to Perl programs. How can I do that?

    解决方案

    The answer to your question is ultimately that you need to use sessions to save state information from one page to the next. CGI::Session::Tutorial is a good for an introduction to the concept and some of the early alternatives.

    Below you will find two scripts that demonstrate this practice. But first.

    1. Always include use strict; and use warnings; at the top of each and every perl script you make. This will make you a better coder and help you find errors quicker. For more reasons check out: Why use strict and warnings?

    2. Because you're using CGI, you should also use CGI::Carp. Simply include the following line after your use CGI; statement: use CGI::Carp qw(fatalsToBrowser);

    3. Your problem is going to be solved by using CGI::Session to save state information between pages. The below scripts save a variable in the first script that is used by the second. You should be able to adapt this to your 3 script scenario.

    4. It's good that you're outputting an http header first thing, but let CGI do that for you. Normally, I'd say use print $q->header();. That will output the same header as you were doing manually. However, because we want CGI::Session to be able to set a cookie, I'll be demonstrating the use of print $session->header(); instead.

    This first script named step1.pl sets a random variable and saves it to the session. Once it's set it provides a link to the next step.

    #!/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>};
    

    And the second script, step2.pl, which requires that the dependentvar is set before proceeding, and then lets the user perform some translations on the string.

    #!/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>};
    

    This should serve as an introduction to the concept of sessions. There are other methods to pass a variable from one web script to the next, but as was discussed in CGI::Session::Tutorial this methods are very fragile. Finally, please read that entire tutorial before applying this solution to your code.

    这篇关于如何在我的Perl程序中访问其他perl程序的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆