Nikhil Hegde Nikhil Hegde - 1 month ago 5
Perl Question

Retaining the value of a variable in Perl

I've a GUI front-end (HTML and JavaScript) a Perl back-end. On clicking a certain button on the home page of the front-end, the Perl back-end receives a specific argument. So this argument leads to change of value of a variable (

$checkInfo
) which is then given back to the GUI. The GUI now pops up a message which displays
$checkInfo
and on closing the pop-up, another page (say page 2) opens. On page 2 is a certain button which emails
$checkInfo
to a certain email address.

The problem I'm facing is that
$checkInfo
is equal to
""
on page 2. On page 1, the back-end returns
$checkInfo = something
to the GUI but that value is lost when I go to page 2. I've tried declaring $value as
state $checkInfo
but that doesn't work either. I use
$checkInfo
only in the
main()
function.

In the code, all you need to know is that
preSubmitCheck::autoSubmitCheck
return the value such that
$checkInfo->{rc} != 0
and in the first iteration
$mode ne 'ticket'
but
$mode eq 'ticket'
in the second iteration. In the second iteration, I need the value of $checkInfo to be the same as the first iteration value.

Perl code:

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 code:

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, "");


I hope this suffices for answering my question. Thanks!

Answer

state only preserves the value of a variable within a single process. When running as CGI, there's a new process for each HTTP request.

If you want to store state across requests, you will need to persist it somewhere that survives across requests -- for instance, by storing it in a cookie, in a session, or in a database.