in reply to Re: Hashes/Scalars and Memory Usage
in thread Hashes/Scalars and Memory Usage

I'm really astounded by both the depth and number of replies :) To fill in the situation a little more, what's going on is I have a screen where I do something like the following x number of times (basically for every field I have ...):
COMPANYNAME: { my $company_name = $cgi->textfield( -name=>'company_name', -default=>$tabdata->{CompanyInfo}->{company_name}, -size=>30 ); $screen->AppendToSection('body',<<"EOF"); <tr> <td class="fieldname">Company Name</td> <td class="fieldvalue">$company_name</td> </tr> EOF }
I ended up settling with the above situation. Each field that's on the form (and these are very complex forms at times..) gets it's own little section like that for clarity. By placing it in the code block, the variable goes out of scope as soon as the block's done...(since I no longer need the variable...) Is this the Wrong Thing to Do(TM)?

Replies are listed 'Best First'.
Re: Re: Re: Hashes/Scalars and Memory Usage
by Fastolfe (Vicar) on Feb 09, 2001 at 02:44 UTC
    In this case I would certainly use a hash. You say you have the above bit of code duplicated several times in your script? This is immediately a sign that your code can probably be re-worked to eliminate the duplication.
    my @FIELDS = ( company_name => 'Company Name', company_addr => 'Company Address', ... ); for (my $i = 0; $i < @FIELDS; $i+=2) { my $field = $cgi->textfield( -name => $FIELDS[$i], -default => $tabdata->{CompanyInfo}->{$FIELDS[$i]}, -size => 30, ); $screen->AppendToSection('body', <<"EOF"); <tr> <td class='fieldname'>$FIELDS[$i+1]</td> <td class='fieldvalue'>$field</td> </tr> EOF }
    If your code varies in more than this respect (e.g. size, the use of another hash key besides CompanyInfo, etc), you could re-structure the @FIELDS array something like this:
    my @ARRAY = ( 'Company Name' => [ 'CompanyInfo' => 'company_name', 30 ], 'Company Address' => [ 'CompanyInfo' => 'company_addr', 60 ], ... );
    Then adapt your code to follow suit..
      To quote something pinkyish: "Astounding Brain, how do you do it?" This is something i've done similarly other places before, and for some reason I had a brainfart and forgot I could do this. Thanks for reminding of something I shouldn't have forgotten to begin with :p This definitely cuts down on Code Bloat(TM), plus makes my code look 3r33t(TM)... Maybe you should author 3r33t perl tricks to live by...