in reply to Hashes/Scalars and Memory Usage

The short answer: Don't worry about it. Use whatever is more readable and whatever makes sense in your application. If you're talking about 30 arbitrary variables built using soft references, for example, I would vote 100% for going with a hash.

The long answer: Any scalar variable is stored in an SV internally. A hash is just a container around a bunch of SV's. I don't know if either method will be necessarily "less efficient" (are you talking about memory or execution time?). On one hand you have the hash lookup to find the value you're looking for in a hash, and on the other hand you have a symbol table lookup for individual scalars (though I guess different variable scoping conventions could make that work differently -- someone else may be able to provide more information).

I honestly don't think there's any serious gain one way or the other, and as a Perl developer you shouldn't have to worry about such a thing, especially with only 30 variables. Use what makes the most sense in your code, but avoid playing tricks with soft references that could come around and bite you in the ass later on. Hope this helps.

Replies are listed 'Best First'.
Re: Re: Hashes/Scalars and Memory Usage
by EvilTypeGuy (Initiate) on Feb 09, 2001 at 01:06 UTC
    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)?
      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...
Re: Re: Hashes/Scalars and Memory Usage
by EvilTypeGuy (Initiate) on Feb 09, 2001 at 01:01 UTC
    This is what I guessed the situation to be... (btw, i'm the anonyomus poster, I just forgot to login that time...)
Re: Re: Hashes/Scalars and Memory Usage
by EvilTypeGuy (Initiate) on Feb 13, 2001 at 03:59 UTC
    I ended up going with the following:
    sub DisplayFields { my ($screen,$screenname,$tabdata,$fields) = @_; for (my $i = 0; $i < @$fields; $i+=2) { my ($field,$style) = ($$fields[$i+1]->[1],''); if ($field eq 'textfield') { $field = $cgi->textfield( -name => @$fields[$i], -size => @$fields[$i+1]->[2], -default => $$tabdata->{$screenname}->{@$fields[$i]}, ); } elsif ($field eq 'picklist') { $field = $cgi->popup_menu( -name => @$fields[$i], -values => @$fields[$i+1]->[2], -labels => @$fields[$i+1]->[3], -default => $$tabdata->{$screenname}->{@$fields[$i]}, ); } elsif ($field eq 'checkbox') { $field = $cgi->checkbox( -name => @$fields[$i], -value => 1, -label => '', -checked => $$tabdata->{$screenname}->{@$fields[$i]}, ); } elsif ($field eq 'textarea') { $style = 'vertical-align: top'; $field = $cgi->textarea( -name=> @$fields[$i], -rows => @$fields[$i+1]->[2], -columns => @$fields[$i+1]->[3], -default => $tabdata->{$screenname}->{@$fields[$i]}, ); } if ($field eq 'begin_table') { $$screen->AppendToSection('body',qq(<table class="@$fields +[$i+1]->[2]"><tr><td>&nbsp;</td></tr>)); } elsif ($field eq 'end_table') { $$screen->AppendToSection('body','</table>'); } else { $$screen->AppendToSection('body',qq(<tr><td class="fieldna +me" style="$style">@$fields[$i+1]->[0]</td><td class="fieldvalue">$fi +eld</td></tr>)); } } }
    So now in my 'tabs', (this is through a web browser don't ask how :p) I can neat things like:
    sub CoreModuleInfoTabEdit { my($module,$tabdata,$screen) = @_; SetupStyles(\$screen); DisplayFields(\$screen,'CoreModuleInfo',\$tabdata,[ begin_table => ['','begin_table','config'], exit_url => ['Exit URL','textfield',50], session_expire_time => ['Session Expire Time (minutes)', +'textfield',4], session_max_time => ['Session Max Time (hours)','textfie +ld',4], allow_guest_login => ['Allow Guest Logins','checkbox','' +], end_table => ['','end_table',''], ]); }
    (in the app i'm working on each subroutine is a different tab, and there's a complex management system that works completely behind the scenes to manage the user's navigation through the tabs)... This works great for me, before I had two screens that totaled 415 lines, after this, both screens plus the display routine totaled 123 lines :) Mmmmm, Diet Code(TM)...

    Thanks Fastolfe!