Loginskip to content

November 2nd, 2006

print “Array : “, procsize() - $base; First

for my $line () { … } because that would read all the lines of the file into a list and then iterate through them one at a time. If you look at that example with scorn, thinking, “But I would never do that,” then how about this? for my $file (grep !/^..?$/, readdir DIR) { … } Looks like an innocent enough standard idiom, and indeed it’s quite safe, as long as you don’t have any directories containing a humongous number of entries. But if you do, be aware that it requires temporary storage for a list nearly twice the size of those entries. If you’d like to bulletproof the code against that contingency, try this: while (defined (my $file = readdir DIR)) { next if $file =~ /^..?$/; … } If you have very large hashes, consider tieing them to a database to trade memory for speed and disk space: use DB_File; # There are several alternatives to this my %large_hash; tie %large_hash, # The hash to tie ‘DB_File’, # The type of database to use ‘database’; # The file to store the database in # use %large_hash… untie %large_hash; # Close the database file However, you could easily abnegate your savings by using a common idiom: for my $key (keys %large_hash) { … } # wrong! because this will cause Perl to form in memory the list of all the keys. Use eachinstead, because it returns only two things at a time (or one, in scalar context): while (my ($key, $value) = each %large_hash) { … } (Although you can’t add or delete hash elements inside the loop.) If your hash values contain references (your hash keys can’t, unless you’re using the Tie::RefHashmodule), then you should use Gurusamy Sarathy’s MLDBM (MultiLevel Database) module from CPAN (http://search.cpan.org/search?dist=MLDBM). Otherwise, the referents to which your references refer will not get stored in the database.
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check Actions tomcat hosting services

Comments are closed.