in reply to Perl Tk variable update question

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.

Replies are listed 'Best First'.
Re^2: Perl Tk variable update question
by perl_seeker (Scribe) on Jan 08, 2010 at 11:45 UTC
    Thank u for your comments and advice.