I'll apologize up front for this being kind of long... I've edited it down to what I hope is clear and concise. I've created a package that contains some subroutines I use on a regular basis. The one I'm having trouble with (
&call_vars) is not working in one instance and I can't figure out why.
When nc_period.pl (below) is called, it initially creates a web page, and &call_vars works nicely (just FYI... &call_vars works nicely on other pages). However, if the user clicks on the "Download to Excel" button on this page, nc_period.pl is called again, the mime Content type is changed, and &call_vars is called again.
The problem is that the variables within &call_vars come out blank/empty after the page is called that second time and outputted to Excel. I think my template is okay because I hard-coded the SQL and the data (numbers/dollars) show up in Excel; however, none of the other variables that I've got stored in hidden fields show up.
--Even if I hard-code one of the values I'm looking for within the if loop that determines if it's
Excel, it doesn't get passed to Excel.
--I also tried something like $summ_descr = $CGI->param('cell_descr'); just to see if that would work, and it
doesn't get passed down to the subsequent portions of the program (right now the SQL is
hard-coded but these variables would actually fill in some of the hard-coded stuff).
--I confirmed that the hidden fields appear on the initial web page (viewed source) and they have
the appropriate values, so &call_vars should have picked them up.
--I've tried removing the $|=1;.
--I tried changing the order of &call_vars and print $CGI->header; (or Excel's mime type), but no luck.
--I've also tried rearranging where &call_vars appears (in the loop, outside the loop).
--I have also triple-checked to make sure that the order of the hidden fields (and TMPL_INCLUDE
where they're stashed) is in the exact order it needs to be to be split out correctly (it is, and
I confirmed it because the &call_vars works correctly on all subsequent and previous pages.
--I was beginning to wonder if there's something about returning the &call_vars values to something
inside a loop that makes it not work, but I put the &call_vars inside the web page's part of the loop
and it works... so I'm not sure what I'm missing.
One last note: I have three pages that work consecutively and all have buttons to download to Excel. Code very similar to what's below works for the first page (both creating the initial web page and creating the download to Excel); it's just not working for this second page and I can't figure out why.
Does anybody see something I'm not? Thanks in advance for any clues you can give me.
nc_period.pl
#!/usr/local/bin/perl5_8
use strict;
use nc_rpt_library;
use HTML::Template;
use DBI;
use CGI ':standard';
my $CGI = CGI->new;
$|=1;
#Determine if this is download to Excel or web report.
my $excel = $CGI->param('excel');
my ($template_name, $summ_row_nobr, $ouc_code_fix, $new_per_fytd );
if ( $excel eq "Download to Excel" )
{ print $CGI->header(-type => 'application/vnd.ms-excel');
&call_vars; #calls in hidde
+n fields
$template_name = "nc_period_xl.tmpl"; }
elsif ( $excel == "" )
{ &call_vars;
print $CGI->header;
$template_name = "nc_period.tmpl";
$summ_descr = $CGI->param('cell_descr'); #these are coming
+from
$summ_col = $CGI->param('cell_col'); #the previous pa
+ge and
$summ_row = $CGI->param('cell_row'); #need to be capt
+ured
}
my $template = HTML::Template->new( filename => $template_nam
+e,
associate => $CGI,
loop_context_vars => 1,
global_vars => 1,
);
#Period drilldown SQL (hard-coded for now) --- snipped out (that par
+t works)
#Fetch Period cell data from database --- snipped out (that par
+t works)
#Create @loop_data array for HTML::Template --- snipped out (that par
+t works)
######################################################################
+#########
#NOTE: LOTS OF VARIABLES SNIPPED OUT HERE but they match the &call_
+vars list.
#For the masochists among you, the entire code is posted on my notepad
+.
######################################################################
+#########
$template->param( title_bar => "Report $rpt_no - $rpt_lbl - Per
+iod",
rpt_no => $rpt_no,
rpt_lbl => $rpt_lbl,
rpt_asofdt => $rpt_asofdt,
rpt_name => $rpt_name,
project => $project,
proj_ref => $proj_ref,
attr_dept => $attr_dept,
attr_descr => $attr_descr,
attr_award => $attr_award,
attr_prog => $attr_prog,
attr_projpd => $attr_projpd,
attr_fyr => $attr_fyr,
attr_budgpd => $attr_budgpd,
attr_fund => $attr_fund,
attr_rate => $attr_rate,
attr_sub => $attr_sub,
attr_status => $attr_status,
attr_spec => $attr_spec,
attr_equip => $attr_equip,
attr_resp => $attr_resp,
attr_pi => $attr_pi,
passdata => \@loop_data,
);
print $template->output();
and the relevant part of my package:
package nc_rpt_library;
#contains various subroutines used by the Campus Financial Reporting s
+ystem
use strict;
use Exporter ();
our @ISA = 'Exporter';
our @EXPORT;
use vars @EXPORT=qw/ &call_vars &commify &dberror $CGI $spec_vars
$titlebar $rpt_no $rpt_lbl $rpt_asofdt $rpt_name $project $proj_re
+f
$attr_dept $attr_descr $attr_award $attr_prog $attr_projpd $attr_f
+yr
$attr_budgpd $attr_fund $attr_rate $attr_sub $attr_status $attr_sp
+ec
$attr_equip $attr_resp $attr_pi $rpt_type $from_act_tbl $from_bud_
+tbl
$per_crmo $per_fytd $per_fytdadj $per_fytdbal $per_fytdbaladj $att
+r_pfyr
$ledger $timespan $bud_led_amt $act_led_amt $pre_led_amt $enc_led_
+amt
$cell_col $cell_row $acct_sql $src_or_scen $curr_or_stat $src_lbl
+$summ_col
$summ_row $per_col $per_row $jrnl_col $jrnl_row $ouc_code $proj_di
+splay
$cell_descr $summ_descr $mycheck $rpt_id $rpt_dates $sql_asofyr $s
+ql_asofmo
$sql_asofdy $amt_type $led_tbl
/;
sub call_vars #pulls in passed variables
{
use CGI ':standard';
my $CGI = CGI->new;
my $spec_vars = param('spec_vars');
#strip out descr labels in front of actual value
$spec_vars =~ s/(xxx\w*zzz)(.*|\n~)(.{1})/$2/gm;
( $titlebar, $rpt_no, $rpt_lbl, $rpt_asofdt, $rpt_name, $project, $pro
+j_ref,
$attr_dept, $attr_descr, $attr_award, $attr_prog, $attr_projpd, $a
+ttr_fyr,
$attr_budgpd, $attr_fund, $attr_rate, $attr_sub, $attr_status, $at
+tr_spec,
$attr_equip, $attr_resp, $attr_pi, $rpt_type, $from_act_tbl, $from
+_bud_tbl,
$per_crmo, $per_fytd, $per_fytdadj, $per_fytdbal, $per_fytdbaladj,
+ $attr_pfyr,
$ledger, $timespan, $bud_led_amt, $act_led_amt, $pre_led_amt, $enc
+_led_amt,
$cell_col, $cell_row, $acct_sql, $src_or_scen, $curr_or_stat, $src
+_lbl, $summ_col,
$summ_row, $per_col, $per_row, $jrnl_col, $jrnl_row, $ouc_code, $p
+roj_display,
$cell_descr, $summ_descr, $mycheck, $rpt_id, $rpt_dates, $sql_asof
+yr, $sql_asofmo,
$sql_asofdy, $amt_type, $led_tbl,
) = split /~\s?/, $spec_vars;
return;
}
updateI did read the latest about that buffer clearing
What does this mean? $!=1 but it doesn't seem to apply to me (but I understand it better now).