logo       
Google Custom Search
    AddThis Social Bookmark Button
-->

Re: [Boston.pm] Odd printing problem: msg#00041

Subject: Re: [Boston.pm] Odd printing problem
>>>>> "AB" == Alex Brelsfoard 
>>>>> <alex.brelsfoard-Re5JQEeQqe8AvxtiuMwx3w@xxxxxxxxxxxxxxxx> writes:

  AB> I'm sure this is something stupid, but it is beyond me.
  AB> Let me show you the snip of code, and then I'll explain what it's supposed
  AB> to be doing:

  AB> --------------------------------------------------------------------
  AB> ...
  AB> 1    foreach my $framID (@$arrayName) {
  AB> 2        # print out the the famID and framID.
  AB> 3        print FILE $famID.",$framID";

        print FILE "$famID.$framID";


  AB> 4        foreach my $trait (@batchOfTraits) {
  AB> 5            # Print the comma delimiter
  AB> 6            print FILE ",";
  AB> 7            # Print the residual if one exists for this trait
  AB> 8            # leave blank otherwise.
  AB> 9            my $traitArrayName = "fullTraitIndex_$trait";
  AB> 10          my $residual = '';
  AB> 11          $residual = $$traitArrayName{$framID} if
  AB> ($$traitArrayName{$framID});

SYMREF ALERT!!

why are you not using a hash of arrays? you are using symrefs which is
just using the symbol table for a data structure but with nasty possible
bugs awaiting you. who know what the real data is there as you can't
even dump it nicely with Data::Dumper.

i would fix that first as i don't see any obviously bad code that would
cause the printing issue.

  AB> 12          print FILE $residual if ($residual);

leaving aside the symref issue, this loop could be reduced to a join/map
and it would be much cleaner. you can use grep to filter out the
undefined values (untested and using a real hash):


        print FILE join ',', map {
                defined( $full_trait_index{$_} ) ?
                        $full_trait_index{$_} : '' }
                @batchOfTraits ;

with 5.9/10 we can use the // op (defined version of ||) which will be
nice.

alse here is a good time to beat on my 'print late' drum.  your sub has
FILE (a symbolic file handle) hard wired which is bad as it ia a global
symbol. you should open the file with a lexical handle. and even then
calling print each time is slow and harder to debug as you can't print
the same output to a file and a terminal. my preference is to build up a
print string with .= and return that. the caller can decide when and
where to print the result including to multiple places or nowhere at
all. also this means the output of such string subs can be used by other
subs if needed. hardwiring print statements into lower level subs locks
you in to just printing that. printing late means you have more control
over printing or using the generated text and it is usually faster as
calls to print are slow (you print maybe only once later on). you can
also use file::slurp to print whole files faster (and with cleaner code)
once you have completed the string.

so remember - print rarely, print late.

rant over.

uri

-- 
Uri Guttman  ------  uri-X/06uaNR9nFmbZtjAW+qKA@xxxxxxxxxxxxxxxx  -------- 
http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


<Prev in Thread] Current Thread [Next in Thread>