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

Hi monks,I have written a small script to open a file using perl tk . Whenever i opened the input file "./test.txt" (original file) consists 10-20 lines of data. If I will open the same file through my script it will show very few lines and it is not saving any changes into original file.

#!/usr/bin/perl -w use warnings; use strict; use Tk; use Tk::FileSelect; my $mw = MainWindow->new; $mw->configure( -background => 'black', -foreground => 'white' ); $mw->geometry( "400x300" ); $mw->title( "Multiple Windows Test" ); my $curr_path = "/root/test.txt"; my $button1 = $mw->Button( -text => "view Results", -background => "cyan", -command => \&button1_sub )->pack( -side => "right" ); $mw->Button( -text => "Exit", -command => sub { exit } ) ->pack( -side => "bottom" ); sub button1_sub { my $subwin1 = $mw->Toplevel; $subwin1->geometry( "500x400" ); $subwin1->title( "Sub Window #1" ); my $fh; open( $fh, '+<', "$curr_path" ) or die $!; my @contents = <$fh>; close( $fh ); my $sublable = $subwin1->Scrolled( 'Text', -scrollbars => 'oso +e', )->pack; $sublable->insert( 'end', @contents ); my $subwin_button = $subwin1->Button( -text => "Close window", -command => [$subwin1 => 'destroy'], )->pack( -side => "bottom" ); #=================Creating save buttion on subwindow =========== my $save_button = $subwin1->Button(-text=>'save', -command =>\&get_save, -background =>'cyan' )->pack(-side=>'right'); } MainLoop; sub get_save { my $dst = $mw->getSaveFile( -initialdir => '/root/', -defaultextension => '.txt', -initialfile =>'test.txt', -title => 'Save', -filetypes => [ [ 'myfiles' => '.txt' ], [ 'All files' => '*' ], ], ); $dst ||= '<undef>'; warn "dst=$dst"; }

Here is my input file "test.txt"

2 3 4 5 6 7 8 9 a b c d e f

If i will open the same file through my script it displays the following contents

1 3 5 7 9 b d f

Replies are listed 'Best First'.
Re: File contents are not displaying in Perl tk
by Corion (Patriarch) on Dec 08, 2010 at 08:28 UTC

    Have you verified that you are reading the data completely?

    Have you verified that you can display a fixed array as you want, without reading the data from the file?

    I don't do Tk, but looking at your data, every second line seems to be missing. This is most likely, because something somewhere interprets your data as hash. Maybe the following line is not how you set the text of a widget?

    $sublable->insert( 'end', @contents );

    As a wild guess, maybe the way to add the information is

    $sublable->insert( 'end', \@contents );
      $sublable->insert( 'end', $_ ) for @contents ;

      Cheers, Chris

        Thank you very much dude....!