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

hello

I have the following code

my $num = 0; for (1..5) { $num ++; my $varbind .= "Varbind " . $num; my $var_frame .= $num; my $varbind_l .= $num; my $varbind_e .= $num; $var_frame = $left_frame_bottom->Frame( )->pack(-side => 'top', fill => 'both',-pady => 2,expand =>'1' +); $varbind_l = $var_frame->Label( -text => "$varbind", )->pack(-side => 'left', -padx => '5'); $varbind_e = $var_frame->Entry( -textvariable => \$value . $num, -width => '15', )->pack(-side => 'right', -padx => '5'); }
How can I reference the text variable?

Replies are listed 'Best First'.
Re: How do I reference a variable?
by l2kashe (Deacon) on Jan 31, 2003 at 18:46 UTC
    Granted it took me a second to find it in your code, but the relevant question would be:
    What do you mean by reference? Find out what is in the attribute? Display the contents of that attribute? actually grab a reference to the scalar contents?

    So with that in mind, and the fact that you are using Tk, there must be a method within the module you are using to extract the data from your object. Probably something along the lines of
    $text = $var_frame->get_attrib(textvariable);
    Or something like that...

    Best place to look would probably be perdoc Some::Module where Some::Module is the name of the module you are 'use'ing at the top of the script

    /* And the Creator, against his better judgement, wrote man.c */
Re: How do I reference a variable?
by Aragorn (Curate) on Jan 31, 2003 at 19:36 UTC
    I don't quite understand your question, but a small unrelated thing: Instead of doing this:
    my $num = 0; for (1..5) { $num++; . . }
    Do this:
    for my $num (1..5) { # You can use $num in the loop, and it gets # increased automatically }
    This keeps the $num variable nicely local to the loop.

    Arjen

Re: How do I reference a variable?
by Aragorn (Curate) on Feb 02, 2003 at 12:21 UTC
    In the documentation of the Tk Entry widget (Tk::Entry) I found the following entry:
    $entry->get Returns the entry's string.

    Maybe this is what you're looking for. Another thing I found was in Tk::options:

    $value = $widget->cget('-option');

    Arjen

Re: How do I reference a variable?
by Anonymous Monk on Feb 02, 2003 at 17:24 UTC
    You can't create different variables like this
    my $var_frame .= $num; my $varbind_l .= $num; my $varbind_e .= $num;
    The best way would be with an array such as @value. Then you can bind the Entry text to each element. Here's an example
    use Tk; use strict; my $left_frame_bottom = MainWindow->new; my @text=qw('' a b c d e); # entry defaults for (1..5) { my $var_frame = $left_frame_bottom->Frame( )->pack(-side => 'top', -fill => 'both', -pady => '2', -expand =>'1'); my $varbind_l = $var_frame->Label( -text => "Varbind ".$_, )->pack(-side => 'left', -padx => '5'); my $varbind_e = $var_frame->Entry( -textvariable => \$text[$_], -width => '15', )->pack(-side => 'right', -padx => '5'); } $left_frame_bottom->Button(-text => 'print_values', -command => sub {print_values()} )->pack(); MainLoop; sub print_values { map { print "Varbind $_ = ",$text[$_],"\n" }(1..5); }
    poj