in reply to Sub call isn't returning values

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 /;
Without completely understanding the rest of your code, I would bet the rent money that this code will be difficult to (a) get working and (b) maintain.

Why do you have so many variables, especially imported from what appears to be data? Are you actually using all the variables as control or summation in the rest of your program logic? If so, you should probably be modularizing your code instead of passing them back to the caller of this module as a big messy blob.

More than likely, what you should be doing instead is exporting a single subroutine that returns a hash ref containing these values, and focussing the logic on treating these values as a bundle, not broken out into 87-gazillion named Perl variables.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re: •Re: Sub call isn't returning values
by Lori713 (Pilgrim) on Mar 02, 2004 at 18:26 UTC
    Thank you for your suggestions. You are, without question, correct. This code is kludgy (at best). However, it does work in every instance but the one described. Given my deadline (which has passed, and testing is to begin with my customers on March 17th), it was the best I could come up with (as a beginner) and still make the boss happy.

    I did try to use CGI::Session but had difficulty in getting that to work (see previous posts). I've been told not to use cookies or put the variables in the URL... so that left me with hidden variables (for now). Once the dust settles, I hope to drop back in to looking at a better way of maintaining persistence with these variables I need to track.

    Believe me, I'm embarrassed by the lack of elegance of the code. I am very aware of how ugly and hard-to-maintain it is right now.

    The variables involve numerous fields (we call them attributes) that appear on each page, plus a lot of them are used to narrow down the SQL criteria for subsequent database calls. If I had more time and more knowledge, I think I'll be able to improve the code to something a little less of an embarrassing first attempt.

    The &call_vars sub was my attempt to bundle all my variables in a concatenated string to pass from page to page via a hidden field. I don't know how create a hash ref and treat the values as a bundle as you suggested.

    Lori

      The following code creates a hash and its reference. It prints from both showing that it is in fact referencing the hash. You should check perldata to get started

      #!/usr/bin/perl use strict; use warnings; my %hidden = (var_name1 => 'var_value1', var_name2 => 'var_value2'); my $hidden_ref = \%hidden; print $$hidden_ref{var_name1}; print $hidden{var_name1};


      Grygonos
Re: •Re: Sub call isn't returning values
by Lori713 (Pilgrim) on Mar 06, 2004 at 00:03 UTC
    Hope you're having a better day today.      ;-)

    It turns out that the code I had posted was working correctly (at least the part that was posted). What I failed to do was put the hidden field in the form containing the buttons that download to Excel. I already had the hidden field in the form containing the cell data. Once I put the hidden field in the buttons form also, it works like a charm. I had tested my template but my assumption that the hidden field would be grabbed no matter where it was in a form was my mistake. I figured this out on my own, using my powers of alchemy.                :-D

    That's not to say there isn't vast room for improvement in my coding abilities. However, I think I should clarify something for you (and get it off my chest) since you roasted me in the Chatterbox a few days ago, suggesting I had no aptitude for programming, should know that and get out of that profession, and that I was dishonest for continuing to accept money for my work when I was so clearly over my head. I'll be honest... it hurt my feelings (yes, I need to develop a thicker skin, but it's tough when someone you respect criticizes you).

    Both you and Abigail-II are among the brightest and most skilled Perl hackers, and are very well-respected in the community. I'd like to say it doesn't matter what you think of me, but truth is, most people look up to you and when you express an opinion, they'll give it a lot more credence than the average person.

    As for what you describe as my "dishonesty" (and something along the lines of getting what you pay for when you pay a consultant $15 an hour), I believe you were assuming I was a consultant who bid on a job for which I was unqualified. I think you later realized I wasn't, so enough said about that. (By the way, I make more than twice that... is that the average beginning consultant's pay?).

    I was assigned this project by my manager; I did not ask for it. He was and is very well aware that I have absolutely no experience in this area, but as leira commented, "sometimes someone has to do it because there's no one else." Since the reports I'm currently re-writing in Perl are mine using another software package and VisualBasic, the task fell to me.

    There are also some political and economic reasons for some of the constraints I'm under. So far, that is the only drawback I have found working for a public university. I have an amazing job and an amazing chain of command; I can't imagine working anywhere else, even with the added stress over the last few months.

    I have discussed on several occasions my discomfort (and plain old inability) to meet the deadlines imposed. However, we are in the middle of a software upgrade and just because one person struggles doesn't mean the whole project gets delayed. I will bust my ass to get it done the best way I know how. That might mean crappy code right now but at least the customers will have something that works accurately. I can (and will) clean up the code later as I become more knowledgeable. It also means that I'll sometimes get roasted for asking stupid questions, but that isn't going to stop me from asking them if that's what it takes to get my job done.

    I tend to be scientific in my approach, testing out different things to see if something will work as expected. Abigail-II refers to that alchemy. However, I continually strive to do my homework, research, and testing up front before posting anything here, lest I get roasted for not doing that. I was merely trying to be thorough even though I didn't expect different results by moving stuff around.

    I am proud of the strides I've made under very difficult, demanding circumstances. Even you wrote that Perl is not an easy language to learn (or something along those lines)... yes, I have your book and I refer to it. As I have mentioned to Abigail-II in a previous post of mine, I learn by doing, not by reading. That may be a serious flaw in a programmer, but by no means a deal-breaker.

    Again, I thank you for the time you took to respond to my post. You didn't have to, and I realize that. I also realize I would be completely lost if folks like you and others hadn't helped me over the past several months, and I am grateful for their continuing support and encouragement.

    Lori