Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Hi, I don't know about your code problem, but as to your original question:
I need help in perl Tk to save, load, default or change config variables, whenever i require. How do I approach.

One good way is to use Storable. It allows you to write an existing hash to a file, OR read the file back into a hash.

Here is some code showing it's basic use. For further info see ztkdb where I use this technique.

use Tk; use Storable; # Say your config information is in notebook.db # and your config hash is %info. # At startup check if notebook.db exists and load it # into your config hash. If it dosn't exist then it means it's the # first run of the program and do something to make the notebook.db my %info; # global hash to hold your data if(-e 'notebook.db'){ %info = %{retrieve('notebook.db')}; # direct to hash }else{ make_notebook() } # now in your program, as you change things and want to # store them to the notebook.db, you need to save it. # Here is a list of Tk related statements that will automatically # save your notebook.db whenever you exit the program #setup auto save of storable db $mw->protocol('WM_DELETE_WINDOW' => sub { &save_it() }); $SIG{__DIE__} = sub { &save_it()}; $SIG{INT} = sub { &save_it()}; $mw->bind('<Control-c>', [sub{&save_it(); Tk::exit;}] ); # and the save_it sub sub save_it{ store(\%info,'notebook.db'); print "saved\n"; }

I'm not really a human, but I play one on earth. ..... an animated JAPH

In reply to Re: Tk save variables into a file and load by zentara
in thread Tk save variables into a file and load by gpremala

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (5)
    As of 2019-11-15 07:35 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Strict and warnings: which comes first?



      Results (80 votes). Check out past polls.

      Notices?