【问题标题】:How to return a hash from a module perl如何从模块 perl 返回哈希
【发布时间】:2012-06-28 13:33:35
【问题描述】:

好的,这个问题困扰了我一段时间。我正在运行一个连接到数据库并从查询中返回值的模块。我有一个脚本调用模块并尝试从模块的子例程返回值。但是由于代码比文字更好,这就是我所拥有的:

sub selectCustomerName ($code){
    connectDB() or die "Failed in subroutine";
    #Selects customer name from customer table where code is $code
    my $selectName = "SELECT * FROM customers WHERE code = ?";
    my $sth = $dbh->prepare($selectName);
    $sth->execute($code);
my $hash = $sth->fetchrow_hashref;
$hash->{customer_name} = $name;
return $name;
$sth ->finish();
$dbh->disconnect();
}

这是我的模块,这是我的脚本:

#!/usr/bin/perl
require Connect;
use warnings;
my $dbh = Connect::connectDB();
my $results = Connect::selectCustomerName('38d');
print $results;

从大量的混乱和切换变量中,我得到了打印 0 和哈希引用,但从来没有打印出哈希的实际值。任何帮助都会非常感谢!

【问题讨论】:

    标签: perl select module dbi subroutine


    【解决方案1】:

    有一些错误。试试这个:

    use strict; use warnings; # never forget this 2 pragmas
    use Data::Dumper; # print what's inside data structures or object
    
    sub selectCustomerName {
        my $code = shift; # or my ($code) = @_; 
    
        connectDB() or die "Failed in subroutine";
        #Selects customer name from customer table where code is $code
        my $selectName = "SELECT * FROM customers WHERE code = ?";
        my $sth = $dbh->prepare($selectName);
        $sth->execute($code);
        my $hash = $sth->fetchrow_hashref;
        print Dumper $hash;
        $name = $hash->{customer_name};
        $sth ->finish();
        $dbh->disconnect();
        return $name;
    }
    
    • 如果您将finish()disconnect() 放在return 之后,它们将永远不会被调用。

    【讨论】:

    • Perl Prototypes (sub selectCustomerName ($code){) 不是语法错误,而是一种指定方法期望的参数的方法。但是,与许多其他编程语言不同,参数不会自动填充:您仍然需要将它们从@_ 中取出,例如通过执行shift
    • 感谢您的快速响应。不过,我仍然遇到打印问题。它从我的脚本中给了我“在打印中使用未初始化的值”。这让我觉得返回的东西有问题,有什么想法吗?
    • 你的意思是 my $name = $hash->{customer_name} 而不是 $hash->{customer_name} = $name; 对吧?
    • sub fu() { } 将声明并定义一个sub,其原型为空,不返回任何内容。如果在第一次使用后看到,perl 会告诉你原型没有效果。 Perl 中的原型不像其他语言中的原型那样做。 如果您不是特别需要并且不知道该功能,请不要使用它们。至于sub selectCustomerName ($code){},那不是一个有效的 Perl 原型。
    【解决方案2】:

    查看您实际拥有的最简单的方法可能是使用Data::Dumper

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    require Connect;
    use Data::Dumper;
    
    my $dbh = Connect::connectDB();
    my $results = Connect::selectCustomerName('38d');
    print Dumper $results;
    

    但是,如果您有一个哈希引用,那么您可以使用 %{$hash_ref} 尊重它,并像使用任何其他哈希一样使用它。

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use 5.010;
    
    require Connect;
    
    my $dbh = Connect::connectDB();
    my $results = Connect::selectCustomerName('38d');
    
    foreach (keys %{$results}) {
      say "Key: $_, Value: $results->{$_}";
    }
    

    【讨论】:

    • Dumper 肯定更容易,但使用其他方法格式化似乎更容易。我确实尝试在我的许多尝试之一中取消引用它,但它什么也没打印。可能是由于我的模块代码中的错误。
    猜你喜欢
    • 1970-01-01
    • 2014-07-02
    • 2012-06-21
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    相关资源
    最近更新 更多