1752565 Members
5354 Online
108788 Solutions
New Discussion юеВ

Re: Perl question

 
SOLVED
Go to solution
Alexander Chuzhoy
Honored Contributor

Perl question

I want to be able to work with variable's value that's another variable's name.For example:

%all=("\$a"=>$buta,"\$b"=>$butb);
for $temp(keys %all) {
if ($all{$temp} eq 1) {
$temp->destroy;
last;
}
}
How can I achieve this?
12 REPLIES 12
Stuart Browne
Honored Contributor

Re: Perl question

Umm, simpler than that.

#!/usr/bin/perl -w
use strict;
my $a = "test";
my $b = "test2";

my $adata = 'one';
my $bdata = 'two';

my %hash = (
$a => $adata,
$b => $bdata
);

for my $key (keys %hash) {
printf( "Got here with %s and %s\n", $key, $hash{$key} );
}
One long-haired git at your service...
Alexander Chuzhoy
Honored Contributor

Re: Perl question

hey ,Stuart.Thanks for your reply.
My problem is kinda more complex.
$a specifies a button.


$a = $mw -> Checkbutton ('-indicator'=>off, '-activebackground'=>green, '-border'=>15, '-text'=>green, -command=>\&action, '-variable'=>\$buta)-> place ( -x=>10, -y=>10, -height=>40, -width=>40);

I wish to destroy this button if someone clicks on it.Of course it's simple if there's only one button.
But I wish to destroy a specific button if someone clicks on it....
Mark Grant
Honored Contributor

Re: Perl question

Why don't you just pass the reference to the button to the "action" function and delete the button there?
Never preceed any demonstration with anything more predictive than "watch this"
Alexander Chuzhoy
Honored Contributor

Re: Perl question

Hey,Mark!
Because this way I'll have to define different function for each button.
I'll do that if there's no choice, but I wish to make the program as smaller as possible.
Ego issues :)
Gregory Fruth
Esteemed Contributor

Re: Perl question

Try wrapping the destroy call in an eval:

eval "$temp->destroy";

Also, the "if ($all{$temp} eq 1)" looks a
bit fishy; perhaps it should be
"if ($app{$temp})".

HTH
Mark Grant
Honored Contributor
Solution

Re: Perl question


You won't need a seperate function for each button. define the command as

[ \&action,$a ] for the first button
[ \&action,$b ] for the second button and so on.

In you action function, destroy $_[0]
Never preceed any demonstration with anything more predictive than "watch this"
Stuart Browne
Honored Contributor

Re: Perl question

Can't you use ${$temp}->destroy ?

I've not done much OO in perl.
One long-haired git at your service...
Alexander Chuzhoy
Honored Contributor

Re: Perl question

Hi,Gregory!
Thanks for the eval tip.
Regarding your second advise:
Also, the "if ($all{$temp} eq 1)" looks a
bit fishy; perhaps it should be
"if ($app{$temp})".


I specifically do
"if ($all{$temp} eq 1)"
because there's also
"if ($all{$temp} eq 2)"

so:
"if ($app{$temp})" won't be good enough
Alexander Chuzhoy
Honored Contributor

Re: Perl question

Stuart, in this particular case -I can't.
I probably miss something...