Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

File changes are not reflecting in perl tk

by vr786 (Sexton)
on Dec 01, 2010 at 10:51 UTC ( [id://874639]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks,I have written a small "perl tk" script to open a file , append the data into the file . The problem comes here is whenever i appended new data into the file those changes are not saving , moreover it is not showing the entire file contents.The behaviour of open a file is abnormal.

File changes are not reflecting into the original file

#!/usr/bin/perl 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 $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, '+<', "./test.txt" ) or die $!; my @contents = <$fh>; # print "@contents\n"; close( $fh ); my $sublable = $subwin1->Scrolled( 'Text', -scrollbars => 'osoe', )- +>pack; $sublable->insert( 'end', @contents ); my $subwin_button = $subwin1->Button( -text => "Close window", -command => sub {$subwin1 => 'destroy'} )->pack( -side => "bottom" ); #=================Creating save buttion on subwindow =========== my $save_button = $subwin1->Button(-text=>'save', -background =>'cyan', -command => sub{ my $dst = $mw->getSaveFile( -initialdir => $ENV{HOME}, -defaultextension => '.in', -title => 'Save', -filetypes => [ [ 'myfiles' => '.in' ], [ 'All files' => '*' ], ], ); $dst ||= "$0.edit"; #in case user hits cancel warn "dst=$dst"; open (my $fh, '>',$dst) or die "$!\n"; print $fh $sublable->get("1.0","end"); } )->pack(-side=>'right'); } MainLoop;

Replies are listed 'Best First'.
Re: File changes are not reflecting in perl tk
by kcott (Archbishop) on Dec 01, 2010 at 11:08 UTC

    The only output to a file you have here is:

    $dst ||= "$0.edit"; #in case user hits cancel warn "dst=$dst"; open (my $fh, '>',$dst) or die "$!\n"; print $fh $sublable->get("1.0","end");

    Your original file is only read:

    my $fh; open( $fh, '+<', "./test.txt" ) or die $!; my @contents = <$fh>; # print "@contents\n"; close( $fh );

    Meaningful variable names would probably help you.

    Update:

    There may also be a problem here:

    my $dst = $mw->getSaveFile( -initialdir => $ENV{HOME}, -defaultextension => '.in', -title => 'Save', -filetypes => [ [ 'myfiles' => '.in' ], [ 'All files' => '*' ], ], );

    Please post the output from your code. My problems in determining what's going wrong here stem from:

    • Opening the original file (./test.txt) in read-write (+<) mode but then only reading it.
    • Whether you're experiencing some confusion with using my $fh twice.
    • Your original file is in the current directory (./) but you're specifying -initialdir       => $ENV{HOME}. What's the full pathnames of these?
    • The extension of your original file is .txt but you're specifying -defaultextension => '.in'
    • Your zig-zag indentation style doesn't help much either.

    -- Ken

      Thanks for giving replay, here is my updated code , my input file "./test.txt" (original file) consists 20-30 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"; }
      </code>

        I asked: "Please post the output from your code.". Is there a reason you can't or won't do that?

        -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://874639]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-18 04:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found