Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Tk with map - Perl newbie

by stickleback (Novice)
on Mar 31, 2011 at 13:25 UTC ( #896626=perlquestion: print w/ replies, xml ) Need Help??
stickleback has asked for the wisdom of the Perl Monks concerning the following question:

I am new to Perl and even newer to Tk. I'm trying to write a login dialog box for a database and learning along the way. I get unexpected behaviour in the following script ...
#/usr/bin/perl use warnings; use strict; use Tk; use Tk 800.000; use Tk::Dialog; my $user = "Hello"; my $password = "World"; my $mw = MainWindow->new(); my $dlgLDL = $mw->Dialog( -title=>'Login', -buttons=>['Ok','Cancel'], -default_button=>'Ok'); my ($fLeft,$fRight) = map {$dlgLDL->Frame(-padx=>4,-borderwidth=>0,-relief=>'groove')- +>pack(-side=>$_,-pady=>5)} qw/left left/; my ($l1,$l2) = map {$fLeft->Label( -text => $_, -anchor=>'e', -width=>12)->pack +(-side=>'top',-pady=>3)} qw/User: Password: /; my ($dfHost, $dfPort, $dfUser,$dfPassword) = map {$fRight->Entry( -textvariable=>$_ )->pack(-side=>'top',- +pady=>3)} qw/\$user \$password/; my $selected = $dlgLDL->Show(); MainLoop;
The -textvariable substitution in the third map statement above doesn't do what I hoped, i.e., set up the link to the associated variable. Instead it seems to just place the quoted text into the Entry field. What am I missing?

Please note this may be a very simple mistake as I am still trying to pick up basic Perl syntax.

Comment on Tk with map - Perl newbie
Select or Download Code
Re: Tk with map - Perl newbie
by vkon (Deacon) on Mar 31, 2011 at 13:40 UTC
    by adding unneeded complexity you've lost what is happening

    why you wrote this way:

    my ($dfHost, $dfPort, $dfUser,$dfPassword) = map {$fRight->Entry( -textvariable=>$_ )->pack(-side=>'top',- +pady=>3)} qw/\$user \$password/;
    you assign 2-element list to 4 variables, so last two are undefined.

    But more to the point, if you'll write

    $fRight->Entry( -textvariable=>\$user )->pack(-side=>'top',-pady=>3 +); $fRight->Entry( -textvariable=>\$password )->pack(-side=>'top',-pad +y=>3);
    your code works as expected.
    I can not explain it, but I know it
    :)

    (addition) qw/\$user .../ does not do what you want

      Thanks for the quick reply.

      The 2 element list to 4 variables was a mistake in extracting the dialog from the larger program. Apologies for that.

      I your suggested broken out code works fine. The following also works ...

      $dfUser->configure( -textvariable=>\$user); $dfPassword->configure( -textvariable=>\$password);
      I can get my code to work and the dialog to display correctly but I wanted to understand why the original code does not work. I am trying to learn Perl along the way.
        (update) deleted wrong content,
        sorry
        :-|
        The quoting operator as in qw/\$user \$password/ doesn't work because what is getting translated in to the $_ variable is the TEXT '\$user', not a reference to $user!

        I show this below with a simple example. Using parens and comma for a normal list allows the \$user to be translated into a value that is the reference. The code will expect to deference the hash value and that will work if what is stored is really a reference to something else rather some text. So in this case, the qw// is not doing what (..,...) does.

        #!/usr/bin/perl -w use strict; use Data::Dumper; my ($user,$password); my $a=0; my %hash1 = map{$a++=>$_}qw/\$user \$password/; #just text my %hash2 = map{$a++=>$_}(\$user, \$password); #real reference print Dumper \%hash1, \%hash2; $user = 234; $password = 'xyz'; print Dumper \%hash1, \%hash2; __END__ $VAR1 = { '1' => '\\$password', '0' => '\\$user' }; $VAR2 = { '3' => \undef, #ref to an undefined val '2' => \undef }; ################# $VAR1 = { '1' => '\\$password', '0' => '\\$user' }; $VAR2 = { '3' => \'xyz', #see changing $user or $password '2' => \234 #did update the hash #because these are real references. };
Re: Tk with map - Perl newbie
by Anonymous Monk on Mar 31, 2011 at 13:41 UTC
    Do you notice anything about how the Tk demo program "widget" makes use of textvariable?

    textvariable takes a reference :)
Re: Tk with map - Perl newbie
by Sinistral (Prior) on Mar 31, 2011 at 15:12 UTC

    As a newbie to both Perl and Tk, I highly recommend the TkDocs site. The tutorial housed there covers multiple OS and multiple programming language issues and comparisons. The Mastering Perl/Tk book is still relevant for the Tk module, but programming best practices have changed and newer Tk libraries/wrappers have been written to take advantage of native operating system widgets to produce better looking and working results.

    Good luck with your programming!

      Thanks for the tips.
      Hi, I would like to know about libraries and wrappers that produce better looking results. Could you provide any links? TkDocs doesn't seem to offer much for Perl TK users.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://896626]
Approved by Courage
Front-paged by Courage
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2014-11-01 10:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (229 votes), past polls