在Perl中保留变量的值

时间:2016-10-21 02:41:31

标签: javascript perl

我有一个GUI前端(HTML和JavaScript)Perl后端。单击前端主页上的某个按钮时,Perl后端会收到一个特定的参数。因此,这个论点导致变量($checkInfo)的值的变化,然后将变量返回给GUI。 GUI现在会弹出一条显示$checkInfo的消息,在关闭弹出窗口时,会打开另一个页面(比如第2页)。在第2页上有一个按钮,可以将$checkInfo通过电子邮件发送到某个电子邮件地址。

我面临的问题是$checkInfo等于第2页的""。在第1页,后端将$checkInfo = something返回给GUI,但该值丢失当我转到第2页时,我尝试将$ value声明为state $checkInfo,但这也不起作用。我只在$checkInfo函数中使用main()

在代码中,您需要知道的是preSubmitCheck::autoSubmitCheck在第二次迭代中返回值$checkInfo->{rc} != 0以及第一次迭代$mode ne 'ticket'$mode eq 'ticket'。在第二次迭代中,我需要$ checkInfo的值与第一个迭代值相同。

Perl代码:

my $username = defined param("username") ? param("username") : undef;
my $action = defined param("run_type") ? param("run_type") : undef;
my $cit_suite = defined param("cit_suite") ? param("cit_suite") : undef;
my $buildroot = defined param("buildroot") ? param("buildroot") : undef;
my $site = defined param("site") ? lc(param("site")) : undef;
my $branch = defined param("branch") ? param("branch") : undef;
my $hw = defined param("hw") ? param("hw") : undef;
my $variant = defined param("variant") ? param("variant") : undef;
my $num_runs = defined param("num_runs") ? param("num_runs") : undef;
my $justification = defined param("justification") ? param("justification") : undef;
my $mode = defined param("userAction") ? param("userAction") : undef;
my $jobID = defined param("jobID") ? param("jobID") : undef;
my $cancel_type = defined param("canceltype") ? param("canceltype") : undef;

    state $checkInfo;
    my $error;
    my %rtn = (
           rc => 0,
           message => "All is well."
           );

    if($mode ne "ticket") {
        $checkInfo = preSubmitCheck::autoSubmitCheck($site,$username,$num_runs);
    }
    if(defined $checkInfo && $checkInfo->{rc} == 0){
        my $target = 1;
    }
    else {
        if($mode eq "ticket"){
            $error = $checkInfo->{message};
            my $rtnFromTicket = sendTicket(
                username => $username,
                cit_suite => $cit_suite,
                action => $action,
                buildroot => $buildroot,
                site => $site,
                branch => $branch,
                hw => $hw,
                variant => $variant,
                num_runs => $original_num_runs,
                justification => $justification,
                errorMessage => $error
               );
            $rtn{rc} = 2;
            $allMessage .= " Your job couldn't be automatically submitted: ". $error;
            $allMessage .= " Email info: ". $rtnFromTicket -> {message};
            $rtn{message} = $allMessage;
            print "00delimiter00";       # use as a delimiter to split from useless print information, and make the front end got the json data.
            print to_json(\%rtn);
            exit $rtn{rc};
        }
        else{
            $error = $checkInfo->{message};
            $rtn{rc} = $checkInfo->{rc};
            if($rtn{rc} == 6) {
                $allMessage .= " Your job couldn't be automatically submitted: ". $error. " You can schedule your run or open a ticket with the SMOKES team.";
            }
            else {
                $allMessage .= " Your job couldn't be automatically submitted: ". $error;
            }
            $rtn{message} = $allMessage;
            print "00delimiter00";       # use as a delimiter to split from useless print information, and make the front end got the json data.
            print to_json(\%rtn);
            exit $rtn{rc};
        }
     }

GUI代码:

var query = "username=<% $ARGS{username} %>";
if(Mode=="all"){
    query += "&run_type=Presub_smoke";
} 
else {
    query += "&run_type=CIT" + "&cit_suite=" + document.getElementById("citsuite").value;
}
query += "&buildroot=" + encodeURIComponent(document.getElementById("buildroot").value);
query += "&site=" + encodeURIComponent(document.getElementById("remoteSite").value);
query += "&branch=" + encodeURIComponent(document.getElementById("branch").value);
query += "&hw=" + encodeURIComponent(document.getElementById("hwtype").value);
query += "&variant=" + encodeURIComponent(document.getElementById("variant").value);
query += "&num_runs=" + document.getElementById("num_runs").value;
query += "&justification=" + encodeURIComponent(document.getElementById("justification").value);
query += "&userAction=" + encodeURIComponent(action);
var submitFcts = new Array();
submitFcts[3] = "updateSubmitInfo";
submitFcts[4] = "return_request";
makeUserRequest2("<% $CGI_form_path %>", query, submitFcts, "");

我希望这足以回答我的问题。谢谢!

1 个答案:

答案 0 :(得分:2)

state仅在单个进程中保留变量的值。当作为CGI运行时,每个HTTP请求都有一个新进程。

如果要跨请求存储状态,则需要将其保留在跨请求存活的某个位置 - 例如,将其存储在cookie,会话或数据库中。