How to access variable of other perl program in my

2020-02-13 02:49发布

问题:

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?

回答1:

There can be more than one way to solve your problem.

  1. You can save the value of $a in a text file and then read that text file in your other two programs.
  2. You can create a module that contains functions to act upon the value of $a. Then you can call the proper moduleName::functionName with $a as argument. A simple tutorial to get you started would be here.
  3. If you want to share variables anyway, you can have a module that acts as some sort of header file with the variable declarations (using the our keyword and/or exporting), and then this module is used in all of your other perl scripts and the variable is visible everywhere. See this answer. For more help on variable scope, you may read this reference: Coping with Scoping


回答2:

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.



标签: html perl cgi