Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从tk perl接口中的子例程传递变量_Perl_Variables_Tk_Subroutine - Fatal编程技术网

从tk perl接口中的子例程传递变量

从tk perl接口中的子例程传递变量,perl,variables,tk,subroutine,Perl,Variables,Tk,Subroutine,我使用的是perl Tk接口,我想在这里有一个按钮test_1,单击这个按钮后,我想将变量$varchoice定义为test_1。如果我按下按钮test_2,变量$varchoice应该定义为test_2 前面是我试图完成此任务的代码片段: $budget_frame->Button(-text => 'test_1',-command => sub{$varchoice=budget_plot_1()})->pack(-side => "left&quo

我使用的是perl Tk接口,我想在这里有一个按钮test_1,单击这个按钮后,我想将变量$varchoice定义为test_1。如果我按下按钮test_2,变量$varchoice应该定义为test_2

前面是我试图完成此任务的代码片段:

$budget_frame->Button(-text => 'test_1',-command => sub{$varchoice=budget_plot_1()})->pack(-side => "left");
$budget_frame->Button(-text => 'test_2',-command => sub{$varchoice=budget_plot_2()})->pack(-side => "left");

sub budget_plot_1()
{
    print "plotting 1\n";
    my $var=1;
    return $var;
}

sub budget_plot_2()
{
    print "plotting 2\n";
    my $var=2;
    return $var;
}

如何调整此代码以获得所需的结果?

您的程序似乎工作正常。下面是我如何测试它的一个示例:

use feature qw(say);
use strict;
use warnings;
use Tk;

my $budget_frame = MainWindow->new(-title=>"Button test");
my $varchoice;

$budget_frame->Button(
    -text => 'test_1',
    -command => sub{ $varchoice = budget_plot_1() }
)->pack(-side => "left");
$budget_frame->Button(
    -text => 'test_2',
    -command => sub{ $varchoice = budget_plot_2() }
)->pack(-side => "left");
MainLoop;
say "Value of \$varchoice = $varchoice";

sub budget_plot_1()
{
    print "plotting 1\n";
    return "test_1";
}

sub budget_plot_2()
{
    print "plotting 2\n";
    return "test_2";
}
输出

Value of $varchoice = test_1

您的程序似乎运行良好。下面是我如何测试它的一个示例:

use feature qw(say);
use strict;
use warnings;
use Tk;

my $budget_frame = MainWindow->new(-title=>"Button test");
my $varchoice;

$budget_frame->Button(
    -text => 'test_1',
    -command => sub{ $varchoice = budget_plot_1() }
)->pack(-side => "left");
$budget_frame->Button(
    -text => 'test_2',
    -command => sub{ $varchoice = budget_plot_2() }
)->pack(-side => "left");
MainLoop;
say "Value of \$varchoice = $varchoice";

sub budget_plot_1()
{
    print "plotting 1\n";
    return "test_1";
}

sub budget_plot_2()
{
    print "plotting 2\n";
    return "test_2";
}
输出

Value of $varchoice = test_1

我假设您在脚本开头声明了
$varchoice
。然后,您的代码似乎可以按照您的要求工作,除了当前将
$varchoice
设置为
1
而不是
test\u 1
。。如果将其设置为
test_1
,它将完全按照您的要求工作。我假设您在脚本开头声明了
$varchoice
。然后,您的代码似乎可以按照您的要求工作,除了当前将
$varchoice
设置为
1
而不是
test\u 1
。。如果将其设置为
test_1
,它将完全按照您的要求工作