如何从另一个子例程访问哈希?

发布于 2024-10-31 02:28:44 字数 1303 浏览 0 评论 0 原文

我正在尝试创建一些用于 Web 测试的脚本,并使用以下代码段从配置文件设置变量:

package setVariables;
sub readConfig{
  open(FH, "workflows.config") or die $!;
  while(<FH>)
  {
    ($s_var, $s_val) = split("=", $_);
    chomp($s_var);
    chomp($s_val);
    $args{$s_var} = $s_val;
    print "set $s_var = $s_val\n";
  } 
  close(FH);
}

例如: var1=val1 var2=val2 var3=val3 等等...

我希望能够将此子例程设置的值传递给另一个包中的子例程。这就是我想要将其传递到的包的内容。 包开始测试; 使用setVariables;

sub startTest{
my %args = %setVariables::args;
my $s_var = $setVariables::s_var;
my $s_val = $setVariables::s_var;

setVariables::readConfig(); #runs the readConfig sub to set variables

my $sel = Test::WWW::Selenium->new( host => "localhost",
                                    port => 4444, 
                                    browser => $args{"browser"}, 
                                    browser_url => $args{"url"} );


$sel->open_ok("/index.aspx");
$sel->set_speed($args{"speed"});
$sel->type_ok("userid", $args{"usrname"});
$sel->type_ok("password", $args{"passwd"});
$sel->click_ok("//button[\@value='Submit']");
$sel->wait_for_page_to_load_ok("30000");
sleep($args{"sleep"});
}

不幸的是,它没有按原样保留变量,我不知道如何引用它们。 感谢您的帮助。

I am trying to create some scripts for web testing and I use the following piece of code to set up variables from a config file:

package setVariables;
sub readConfig{
  open(FH, "workflows.config") or die $!;
  while(<FH>)
  {
    ($s_var, $s_val) = split("=", $_);
    chomp($s_var);
    chomp($s_val);
    $args{$s_var} = $s_val;
    print "set $s_var = $s_val\n";
  } 
  close(FH);
}

for example: var1=val1
var2=val2
var3=val3
etc...

I want to be able to pass the values set by this subroutine to a subroutine in another package. This is what I have for the package I want it passed into.
package startTest;
use setVariables;

sub startTest{
my %args = %setVariables::args;
my $s_var = $setVariables::s_var;
my $s_val = $setVariables::s_var;

setVariables::readConfig(); #runs the readConfig sub to set variables

my $sel = Test::WWW::Selenium->new( host => "localhost",
                                    port => 4444, 
                                    browser => $args{"browser"}, 
                                    browser_url => $args{"url"} );


$sel->open_ok("/index.aspx");
$sel->set_speed($args{"speed"});
$sel->type_ok("userid", $args{"usrname"});
$sel->type_ok("password", $args{"passwd"});
$sel->click_ok("//button[\@value='Submit']");
$sel->wait_for_page_to_load_ok("30000");
sleep($args{"sleep"});
}

Unfortunately its not holding on to the variables as is and I don't know how to reference them.
Thank you for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

隔纱相望 2024-11-07 02:28:44

您的代码有一些问题。让我们先解决这些问题。

# Package names should start with upper case unless they are pragmas.
package SetVariables;

# Do this EVERYWHERE.  It will save you hours of debugging.
use strict;
use warnings;

sub readConfig{

  # Use the three argument form of open()
  open( my $fh, '<', "workflows.config") 
    or die "Error opening config file: $!\n";

  my %config;

  # Use an explicit variable rather than $_
  while( my $line = <$fh> )
  {
    chomp $line;    # One chomp of the line is sufficient.
    ($s_var, $s_val) = split "=", $line;

    $config{$s_var} = $s_val;
    print "set $s_var = $s_val\n";
  }

  close $fh;

  return \%config;
}

然后像这样使用:

use SetVariables;

my $config = SetVariables::readConfig();

print "$_ is $config->{$_}\n" 
  for keys %$config;

但不要自己做这一切,而是检查 CPAN。考虑Config::AnyConfig::IniFiles, 配置::JSON


您在评论中指出,您正在尝试使用多个文件、主要代码和几个包。

一种常见的模式是在主代码中加载配置并将其(或选择其中的元素)传递给使用代码:

package LoadConfig;

sub read_config {
    my $file = shift;

    my $config;
    # Do stuff to read a file into your config object;

   return $config;
}

1;

同时在另一个文件中:在

package DoStuff;

sub run_some_tests {
    my $foo = shift;
    my $bar = shift;

    # Do stuff here

    return;
}

sub do_junk {
    my $config;

    my $foo = $config->{foo};

    # Do junk

    return;
}

1;

主脚本中:

use DoStuff;
use LoadConfig;


my $config = LoadConfig::read_config('my_config_file.cfg');

run_some_tests( $config->{foo}, $config->{bar} );
do_junk( $config );

因此在 run_some_tests() 我从配置中提取了几个元素并将它们单独传递。在 do_junk() 中,我只是传入整个配置变量。

Your code has some problems. Let's fix those first.

# Package names should start with upper case unless they are pragmas.
package SetVariables;

# Do this EVERYWHERE.  It will save you hours of debugging.
use strict;
use warnings;

sub readConfig{

  # Use the three argument form of open()
  open( my $fh, '<', "workflows.config") 
    or die "Error opening config file: $!\n";

  my %config;

  # Use an explicit variable rather than $_
  while( my $line = <$fh> )
  {
    chomp $line;    # One chomp of the line is sufficient.
    ($s_var, $s_val) = split "=", $line;

    $config{$s_var} = $s_val;
    print "set $s_var = $s_val\n";
  }

  close $fh;

  return \%config;
}

Then use like so:

use SetVariables;

my $config = SetVariables::readConfig();

print "$_ is $config->{$_}\n" 
  for keys %$config;

But rather than do all this yourself, check out the many, many config file modules on CPAN. Consider Config::Any, Config::IniFiles, Config::JSON.


You note in your comment that you are trying to work with multiple files, your main code and a couple of packages.

One pattern that is common is to load your config in your main code and pass it (or select elements of it) to consuming code:

package LoadConfig;

sub read_config {
    my $file = shift;

    my $config;
    # Do stuff to read a file into your config object;

   return $config;
}

1;

Meanwhile in another file:

package DoStuff;

sub run_some_tests {
    my $foo = shift;
    my $bar = shift;

    # Do stuff here

    return;
}

sub do_junk {
    my $config;

    my $foo = $config->{foo};

    # Do junk

    return;
}

1;

And in your main script:

use DoStuff;
use LoadConfig;


my $config = LoadConfig::read_config('my_config_file.cfg');

run_some_tests( $config->{foo}, $config->{bar} );
do_junk( $config );

So in run_some_tests() I extract a couple elements from the config and pass them in individually. In do_junk() I just pass in the whole config variable.

这样的小城市 2024-11-07 02:28:44

您的用户会看到配置文件还是只是程序员?如果只是程序员,请将您的配置放入 Perl 模块中,然后使用 use 导入它。

如果您正在编译程序,则仅使用程序员的配置文件的唯一原因。由于 Perl 程序是脚本,因此不必担心解析配置文件的开销;就像 Perl 那样做。

除非它是给你的用户使用的并且它的格式比 Perl 简单。

PS:已经有一个名为Config的模块。调用您的 My_config 并按如下方式加载它:

use FindBin '$RealBin';
use lib $RealBin;
use My_config;

请参阅:
perldoc FindBin
perldoc 配置

Are your users going to see the configuration file or just programmers? If it's just programmers, put your configuration in a Perl module, then use use to import it.

The only reason to use a configuration file for only programmers if you are compiling the program. Since Perl programs are scripts, don't bother with the overhead of parsing a configuration file; just do it as Perl.

Unless it's for your users and its format is simpler than Perl.

PS: There's already a module called Config. Call yours My_config and load it like this:

use FindBin '$RealBin';
use lib $RealBin;
use My_config;

See:
perldoc FindBin
perldoc Config

绳情 2024-11-07 02:28:44

我建议使用常规格式,例如 YAML 来存储配置数据。然后,您可以使用 YAML::LoadFile 读回配置数据的哈希引用并然后使用它。

或者,如果您不想将 YAML 或某些其他配置格式与预先编写的模块一起使用,则需要您的读取例程实际返回哈希或 aa hashref。
如果您需要更多背景信息,请查看 perlrefperlreftutperlintro

I would suggest using a regular format, such as YAML, to store the configuration data. You can then use YAML::LoadFile to read back a hash reference of the configuration data and then use it.

Alternatively, if you don't want to use YAML or some other configuration format with pre-written modules, you'll need for your reading routine to actually return either a hash or a a hashref.
If you need some more background information, check out perlref, perlreftut and perlintro.

小姐丶请自重 2024-11-07 02:28:44

您需要做的就是收集哈希中的变量并在 readConfig:

my %vars = ( var1 => val1,
             var2 => val2,
             var3 => val3,
           );

return \%vars;

和 startTest: 中返回对它的引用

my $set_vars = setVariables::readConfig();

all you need to do is collect the variable in a hash and return a reference to it in readConfig:

my %vars = ( var1 => val1,
             var2 => val2,
             var3 => val3,
           );

return \%vars;

and in startTest:

my $set_vars = setVariables::readConfig();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文