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

Hello:

The logic flow in my program goes this way
my $mw = MainWindow->new; $mw->geometry("500x250"); /*Other stuff*/ $mw->Button(-text=>'OK',-background=>'blue',-command=>\&ok_but)->pack( +-side=>'top',-pady=>2); MainLoop; sub ok_but { $receiptno=40; /*Other stuff*/ $sw->Button(-text=>'Deposit work receipt',-background=>'blue',-command +=>\&deposit)->pack(-side=>'top',-pady=>2); sub deposit{ my $dw = MainWindow->w; $dw->geometry("500x700"); /* Entry boxes and lists where user enters/selects data for receipt generation */ $dw->Button(-text=>'Print receipt',-background=>'blue',- command=>\&d +eposit_fwrite_print)->pack(-side=>'top',-pady=>2); } sub deposit_fwrite_print{ /* Write data to text file and print recipt receipt using Win32::Printer::Direct; */ $receiptno=$receiptno +1; } } End of subroutine ok_but
The user clicks on the 'Print receipt' button and receipt gets printed. But suppose the user wants to print the receipt again and clicks on the button in the same open window a second time, the $receiptno gets updated and the receipt number for the same set of data is printed incorrectly (number +1).Also the same set of data is written to the text file again.

How to ensure that $receiptno is updated and data is written to text file only if a new set of data is entered by the user?

Also once u exit the program the value of $receiptno gets lost.How do we ensure that each time the program is invoked $receiptno is set to last value used + 1?

I have thought of solutions to all the above but was wondering if someone could suggest something more efficient?

Thanks for reading,
perl_seeker

Replies are listed 'Best First'.
Re: Perl Tk variable update question
by zentara (Cardinal) on Jan 06, 2010 at 11:38 UTC
    Those are alot of "hows".... its almost like you are asking us to do your homework.

    Since you did not give a running code example, your code fragments are not clear if you are nesting subroutines or not.

    ensure that $receiptno is updated and data is written to text file only if a new set of data is entered by the user?

    There are probably more elegant ways of doing it, but one way is to keep a list of all the $receiptnos that get printed out, and check that list before any new print.

    If the idea is to stop multiple simultaneous prints, set the button state to disabled at the start of the print routine, and set back to normal at end of the print sub.

    This example shows the idea

    #!/usr/bin/perl use warnings; use strict; use Tk; my $FILTER_IS_ON = 0; my $top = new MainWindow(); my $c1 = $top->Button(-text=>$FILTER_IS_ON ? 'turn off' : 'turn on', -command => \&turn)->pack(); my $c2 = $top->Button(-text=> 'Go', -state => 'disabled', -command => sub { print "ook!\n" })->pack(); MainLoop(); sub turn { $FILTER_IS_ON = $FILTER_IS_ON ? 0 : 1; $c1->configure(-text=> $FILTER_IS_ON ? 'turn off' : 'turn on'); $c2->configure(-state=> $FILTER_IS_ON ? 'normal' : 'disabled'); }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Perl Tk variable update question
by graff (Chancellor) on Jan 07, 2010 at 06:20 UTC
    Incrementing the receipt number is supposed to be something that happens when a receipt is created and stored. Printing a record really has nothing to do with creating a new record, so incrementing to the next record number should not be part of the print function.

    If you are keeping track of receipt numbers together with associated data in a text file, I would assume that this is not a "write-only" file. Does your Perl/Tk app also provide a means of reading back from the file?

    If so, then you should make the access to that file "modular": create a separate module for i/o on that file, so that you have a clean, simple set of methods to create a new record, fetch back the last record, determine the next available receipt number, and search for an arbitrary receipt (by number or by other data content).

    Or maybe you'd rather use some sort of database instead, or at least a DBM file tied to a hash in your script (check out AnyDBM_File), using the receipt number as the key value in the hash or receipt table.

    But the main thing is: write your code so that the receipt number is only incremented as part of creating a new record, not as part of printing anything.

      Thank u for your comments and advice.