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

Hi, I have written a simple Perl/Tk application. In this application I let users to view a text file. When users try to open a large file, they complain that it takes too long to open the file comparing to other text editors. I have decided to remove this function (opening a text file to view) from my application. Before I remove it I just want to find out if there is an elegant and fast way of opening a file in text widget. My code opens the file reads each line and inserts it into text widget, and apparently that takes too long. Any suggestions?

Replies are listed 'Best First'.
Re: text widget in Perl/Tk question
by DigitalKitty (Parson) on Oct 30, 2002 at 06:02 UTC
    Hi Anonymous Monk,

    Could you post the code you are using?
    You can read a file and place each line into the text widget as it is being read with this:

    $text = $mw->Scrolled("Text")->pack(); open( FILE, "MyText" ) or die "Arghh...$!\n"; while( <FILE> ) { $text->insert('end', $_ ); } close FILE;


    Hope this helps,

    -Katie.
      Yes the code above is what I am using at the moment but the problem is when I open the same file in TextPad for instance, TextPad opens it in no time whereas when I use the code above in Perl/Tk, it takes ages to open it. Is this the limitation of the text widget and can I do something about it? Thanks
        Well, certainly each call to $text->insert takes time. You can do it with just one call, like this:
        { local $/; $text->insert('end', <FILE> ); }
        That will insert the entire file at once.

        However, if the file is really large, you will always have problems trying to hold the entire file in memory at once. Good text editors are smart enough to page out the parts of the file not being viewed, in an efficient manner. You may have to resort to this kind of trick.

Re: text widget in Perl/Tk question
by opolat (Novice) on Oct 31, 2002 at 04:49 UTC
    Hi, You could also try to set special $/ to a null string; $/ = "";, then perl read paragraph of text, including new lines.
    $/ = ""; while(<FH>) { # $_ will contain the entire paragraph, more than just a line $MainText->insert("end", $_); }
    This should speed up loading the text into the widget.