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

Hello Monks I am trying to save multiple files in Perl TK using Tk::Notebook in a textbox , the problem i am facing is that i am not able to save the edited files. The reason most likely is because my textbox variable remember only latest open file.Can anyone give me idea how to get around this issue

use strict; use warnings; use diagnostics; use Tk; use Tk::NoteBook; my @types=(); my $file_array=undef; my ($page,$content)=undef; my $mw = MainWindow -> new(-title => "Testing"); $mw->Button(-text => "Open", -command =>\&fopen)->pack(-anchor => 'cen +ter', -side=>"left"); $mw->Button(-text => "Save", -command =>\&save)->pack(-anchor => 'cent +er', -side=>"left"); my $nb = $mw->NoteBook(-background=> 'white', -inactivebackground => ' +grey', )->pack(-expand => 1, -fill => 'both'); my $maintab="Main Window"; my $frame = $nb->add($maintab,-label => $maintab); my $txt=$frame-> Text(-width=>240, -height=>50)->pack(); MainLoop; sub file_open { @types =( ["Log files", [qw/.log/]], ["Text files", [qw/.txt/]], ["All files", '*'], ); my $aref = $mw->getOpenFile( -filetypes => \@types , -initialdir => '.', -multiple =>1 ); return($aref); } sub fopen { $file_array=&file_open; print "\n"; print join("\n", @$file_array); print "\n"; for my $tab (@$file_array){ my $frame = $nb->add($tab,-label => $tab); $txt=$frame-> Text(-width=>240, -height=>50)->pack(); open FR, "<$tab" or die "Cant open file:$!\n"; $content=join "", <FR>; $txt->insert('end',"$content"); close(FR); } } sub save { $page=$nb->raised(); $content=$txt->get("1.0", "end"); print "\nsaving content= $content\n"; open FW,">$page" or die "Cant Write to file:$!\n"; print FW $content; close(FW); }

Replies are listed 'Best First'.
Re: Perl Tk Multiple Notebook Editing and Save
by zentara (Cardinal) on Oct 07, 2014 at 10:36 UTC
    You might want to look at ztkdb where I save multiple text widgets in different tabs. The basic idea is to make a hash for each tab, and store the associated text widget object in the tab's hash entry. Then to save them all, you do something like this:
    foreach my $tabname(keys %tabs){ my $savetxt = $tabs{$tabname}{'textbox'}->get("1.0","end"); chomp $savetxt; # now save it wherever you want, open a file or write to a databa +se.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Great, thanks that works for me, as you suggested created hash for tabs and associated them with textbox and now i am able to edit and save them

Re: Perl Tk Multiple Notebook Editing and Save
by Anonymous Monk on Oct 07, 2014 at 00:00 UTC

      Thanks for pointing them out. I have fixed those errors as you suggested. However in the actual code, which variable are you suggesting to switch to my vars. whenever a tab is raised i want to edit that file and save it. i tried but cant figure out where is the mistake or what am i missing. any help would be appreciated.

        Thanks for pointing them out. I have fixed those errors as you suggested. However ...

        How did you fix them?

        Here is how I fixed them

        I run the program, open two three files, and two three new tabs are added, I hit save on each tab and the correct filename is selected/warned, not just the latest ...

        i tried but cant figure out where is the mistake or what am i missing.

        What I said, subs shouldn't use global variables, they should pass arguments, its Coping with Scoping also known as the spirit of strict also known as the life cycle of variables or memory management