Just a little less work gets you a crude GUI version:

use strict; use warnings; use Tk; my $obj = bless {}; # Create an object to make passing parameters easi +er and to # maintain state. Generally considered better than using global va +riables. if (open my $tempIn, '<', "DB.txt") { while (<$tempIn>) { chomp; my ($name, $number) = split ':'; next unless defined $number; #Skip empty or corrupt lines $obj->{phoneBook}{$name} = $number; } close $tempIn; } $obj->{main} = MainWindow->new (-title => 'My Simple Phone Book'); $obj->{main}->Button (-text => 'List', -command => [\&doList, $obj])-> +pack (); $obj->{main}->Label (-text => "Name:")->pack (); $obj->{main}->Entry (-textvariable => \$obj->{name})->pack (); $obj->{main}->Button (-text => 'Del', -command => [\&doDel, $obj])->pa +ck (); $obj->{main}->Label (-text => "Number:")->pack (); $obj->{main}->Entry (-textvariable => \$obj->{number})->pack (); $obj->{main}->Button (-text => 'Add', -command => [\&doAdd, $obj])->pa +ck (); $obj->{main}->Button (-text => 'Exit', -command => [\&doExit, $obj])-> +pack (); MainLoop (); sub doAdd { my ($self) = @_; $self->{phoneBook}{$self->{name}} = $self->{number}; } sub doExit { my ($self) = @_; open my $out, '>', "DB.txt" or die "Sorry Cant Open File: $!"; for my $name (sort keys %{$self->{phoneBook}}) { print $out "$name:$self->{phoneBook}{$name}\n"; } close $out; exit; } sub doDel { my ($self) = @_; delete $self->{phoneBook}{$self->{name}}; } sub doList { my ($self) = @_; my $msg; for my $name (sort keys %{$self->{phoneBook}}) { $msg .= sprintf "%-20s %20s\n", $name . ':', $self->{phoneBook +}{$name}; } $self->{main}->messageBox ( -icon => 'info', -message => $msg, -title => 'Phone numbers', -type => 'Ok', ); }

Note that the data is maintained in memory until the program exits.


Perl's payment curve coincides with its learning curve.

In reply to Re: Phone Book by GrandFather
in thread Phone Book by h4ckroot

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.