use warnings;
use strict;
use Tk;
my $mw = MainWindow->new (-title => "editor");
my $text = $mw->Scrolled
('Text', -font => 'normal', -wrap => 'word', -scrollbars => 'e',);
$text->pack ();
$mw->Button (-text => "open file", -command => \&openFile)->pack ();
MainLoop ();
sub openFile {
my @filetypes = (
['Text', '.txt', 'TEXT'],
['Text', '.txt', 'TEXT'],
);
my $currentFile = $text->getOpenFile
(-defaultextension => '.txt', -filetypes => \@filetypes);
}
Self modifying code is generally frowned on as being somewhat difficult to maintain. A setting file would generally be the best way to store persistent data, but there are lots of ways of generating and maintaining the settings file. One fairly easy way to do it is ti use Storable; and keep all your context stuff in a hash. Then you can:
use Storable;
# Load persistent stuff
my $persistRef = retrieve ('config') if -e 'config';
# Use stuff
my $text = $persistRef->{text} || '';
# ...
$persistRef->{text} = $text;
# Save stuff
store $persistRef, 'config';
Update: added file open dialog code to Tk sample
DWIM is Perl's answer to Gödel
|