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

I have a peculiar problem using module Tkx in combination with module Storable. The purpose is to store all parameters entered by a user in a Tk interface in a file so that it can be loaded the next time when the program is started again.

I have simplified the code here, there is only one parameter $Value and a simple Tk screen with only one entry field linked to $Value. I first store and retrieve $Value and this works OK. Then I change the value with the Tk screen and then things go strange:

my $Value = 666; store( \$Value, 'param.ini' ); # The file viewed in Wordpad shows 'pst0 12345678 ' my $pRPar = retrieve('param.ini'); $Value = $$pRPar; print("$Value\n"); # Prints '666' my $MainWindow = Tkx::widget->new('.'); my $Entry = $MainWindow->new_ttk__entry(-textvariable => \$Value); $Entry->g_grid(-row => 1, -column => 1); Tkx::MainLoop(); # Change the value into '111' and close the window print("$Value\n"); # Prints '111', so this is OK. But... store( \$Value, 'param.ini' ); # The file viewed in Wordpad shows now 'pst0 12345678Tcl:: +Var Tcl^ ::perl::SCALAR(0x3b54b38)'

So the first time the value is stored correctly (if you use a string instead of an integer you will see the string value in the file). The second time you see strange things appear such as 'Tcl::Var' and '::perl::SCALAR(0x3b54b38)'.

Any idea what's happening here?

Replies are listed 'Best First'.
Re: Strange behaviour of Tkx in combination with Storable
by aitap (Curate) on Jan 03, 2014 at 20:36 UTC
    That's probably because your $Value becomes an object with overloaded stringification, so it prints 111, but really is a reference to a scalar blessed into Tcl::Var package. Try storing "$Value" instead of just $Value to force stringification and store only the value you need.
      Thanks aitap.

      What I forgot to mention explicitely is that Storable crashes when retrieving the references to the Tcl::Var objects.

      I could avoid this by copying all parameters to new variables first and store those, but this is rather ugly. Do you see another way to make Storable see the correct data structure?

        You can make a reference to a stringified copy of your variable on the fly: store \"$Value", $filename, or, if you are storing a data srtucture, store { var => 1, anothervar => "$Value", ... }, $filename.

        Quotes explicitly transform anything into a string, and no object can survive that.

Re: Strange behaviour of Tkx in combination with Storable
by zentara (Cardinal) on Jan 05, 2014 at 12:07 UTC
    You might be interested in looking at ztkdb, where I do essentially the same thing with Tk instead of Tkx.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh