iSina has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have written a program that reads pdb files (protein databank) interprets some values and prints back data into a file. Because I need my data without closing the program I use autoflush. The method seemed to work well with my sample file. But when I switched to other pdb files for bug testing, the autoflush did not work. It wrote data halfway through into the output and wrote the remaining when I close the program... What might be the reason for this behaviour (I checked autoflush many times) ? Does the program enter some kind of loop somewhere? Thanks a lot...

Replies are listed 'Best First'.
Re: Autoflush/Buffer Problem
by moritz (Cardinal) on Oct 06, 2007 at 20:36 UTC
    That's rather hard to tell without seeing any code.

    Did you select the file handle before changing $|? or are you using another method to change the buffering behaviour?

    Do you close your file handles after you finish writing to them?

      I would like to post the code here but the data reading part is rather long. However I can post the dataprinting function where the problem may have occured? I use autoflush, yes I close my files. and the strange thing is this problem does not occur with some input and it occurs with others.. What I put below is only a short part of the program. Other relevant part could be the part where the pdb file is read from the given file but it seems like a writing buffer rather than reading buffer...
      mainbody { my $index=0; my $input=$mw->getOpenFile(); open FILE, ">$input" or die $!; FILE->autoflush(1); #someother direct printings into file(direct meaning printing i +nto file within this body without calling other functions as below) tie @lines, 'Tie::File', $input or die $!; #somemore direct printings into file #below are functions that get arrays, some indices and prints them + into file if($pdb_active) { printtoDataBase(\@exp_beta_alpha,16,6); #below printtoDataBase(\@exp_beta_heavy,32,6); } #other printing functions untie @lines; close FILE; } sub printtoDataBase #the part where the problem occurs { my ($dataArray,$column,$ref_size)=@_; my $index=0; $repeat=10; my @dataArray=@$dataArray; #deref for (;$repeat<($residuenumber+10); $repeat++) { my $size = length $dataArray[$index]; if($size>($ref_size)) { $size=($ref_size); } #refsize tells up to how many digits do the user w +ant the number to be displayed substr($lines[$repeat],($column),($size))= substr($dataArray[$index],0,($size)); $index++; } # above function gets the b-factor numbers in the file found + the stated part by the substr }
Re: Autoflush/Buffer Problem
by shmem (Chancellor) on Oct 06, 2007 at 20:36 UTC
    welcome to the Monastery, iSina!
    Does the program enter some kind of loop somewhere?
    Perhaps. Hard to tell without code to look at. See How (Not) To Ask A Question.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Err that was me, I forgot to log in sorry.
Re: Autoflush/Buffer Problem
by mwah (Hermit) on Oct 07, 2007 at 21:42 UTC
    iSinaWhat might be the reason for this behaviour

    from your code posted as Anonymous (Re^2: Autoflush/Buffer Problem), I can see that you
    use Tie::File on an already opened file:
    ... my $input = $mw->getOpenFile(); open FILE, ">$input" or die $!; FILE->autoflush(1); ... tie @lines, 'Tie::File', $input or die $!; ...
    This is, according to the Tie::File documetation,
    not supported. You have to provide the filehandle:
    ... open FILE, ">$input" or die $!; tie @array, 'Tie::File', \*FILE, ...; ...
    From your code fragment posted it can be seen
    that there are at least one zillion interactions
    with other (unposted) code parts that may contain bugs ;-)

    So one can only wish that the "force may be you" in
    cleaning up this program eventually ...

    Regards

    mwa
      Yes I do have lots of other functions but they are rather discrete. So most of the part I have to individually fix them if something goes wrong. Thanks though :/ And as far as what you said, I didnt know of such a thing so I tried to close the file before using tie but it seemed to produce no effect. Still the last remaining of my data is kept in buffer until I close the program... And for the other part, I tried using \* without closing the file before hand but this time the tie didnt work and the array wasnt created. Strange :/ I will read more on the link you have given, thanks.