【问题标题】:Modules and calling subroutines in PerlPerl 中的模块和调用子例程
【发布时间】:2016-06-05 15:06:12
【问题描述】:

我在让特定子例程工作时遇到问题,我有我的模块“module1.pm”和我的测试脚本“testmodule.pl”。下面是 testmodule.pl 的代码:

#!/usr/bin/perl

use File::Basename qw(dirname);
use Cwd qw(abs_path);
use lib dirname(dirname abs_path $0) . '/perl/lib';
use warnings;
use strict;
use Report::module1 qw(userloggins PasswordAge DiskUsage);

#Gets the users from module1::userloggins() and sorts them
my $PasswordAge;
my %userhash = Report::module1::userloggins ();
print "Here are the users that have logged in and how many times they've logged in.\n";
foreach my $user (sort { $userhash{$b} <=> $userhash{$a}  ||  $a cmp $b } keys(%userhash)) {

print("$user: $userhash{$user}\n");
}
print "----------------------------------------------\n";

#PasswordAge

print "How many days would you like to check users for old passwords?\n";
my $input = <>;
my @UserPassChange = PasswordAge (int($input), keys(%userhash) );
for my $i (0...$#UserPassChange) {
print ("User $UserPassChange[$i] has not changed their password in $input days.\n");
} 
print "----------------------------------------------\n";

#DiskUsage
print "Input size for checking users home folder: ";
my $input2 = <>;
my %DiskUsageReturned = Report::module1::DiskUsage (int($input2),     keys(%userhash) );
#print %DiskUsageReturned;
foreach my $user (%DiskUsageReturned){
print("$user: $DiskUsageReturned{$user}\n");
}

这是module1.pm的代码:

#!/usr/bin/perl


use warnings;
use strict;
package Report::module1;
use Exporter qw(import);
our @EXPORT_OK=qw(userloggins PasswordAge DiskUsage);

sub DiskUsage {

my $userInput = shift @_;
my @users = @_;
my %DiskUsageHash;

foreach my $user (@users){
print "$user";
my $UserDiskUsage = int(`du -bs /home/$user | cut -f1`);
my $Mebi = int ($UserDiskUsage / 1048576);
if ($Mebi >= $userInput){
    #print @UserDiskUsage;
$DiskUsageHash{$user} = $Mebi;
}
}


return %DiskUsageHash;

}

1;

在我的模块中使用子例程时,我似乎无法弄清楚我做错了什么。它使用比我输入的任何内容都大的主文件夹获取用户,但我也收到很多错误消息。这是我运行“testmodule.pl”时得到的输出。

Input size for checking users home folder: 5
maholilaelita15vicnigigaaddahepemaskmaho: 14
Use of uninitialized value in concatenation (.) or string at testmodule.pl line 36, <> line 2.
14: 
lila: 9
Use of uninitialized value in concatenation (.) or string at testmodule.pl line 36, <> line 2.
9: 
giga: 7
Use of uninitialized value in concatenation (.) or string at testmodule.pl line 36, <> line 2.
7: 
elit: 123
Use of uninitialized value in concatenation (.) or string at testmodule.pl line 36, <> line 2.
123: 
adda: 65
Use of uninitialized value in concatenation (.) or string at testmodule.pl  line 36, <> line 2.
65: 
hepe: 188
Use of uninitialized value in concatenation (.) or string at testmodule.pl  line 36, <> line 2.
188: 
mask: 94
Use of uninitialized value in concatenation (.) or string at testmodule.pl   line 36, <> line 2.
94: 

【问题讨论】:

  • 好吧,第一个代码 sn-p 甚至无法编译,因为 %userhash 没有定义。两者都少于 36 行,所以这也不是正确的错误消息。
  • @Sobrique 那里,我修好了。没有复制整个代码。
  • @nillenilsson 你的缩进真的那么糟糕,还是只是复制和粘贴的产物?

标签: perl module


【解决方案1】:

你的问题是这样的:

foreach my $user (%DiskUsageReturned) {
    print("$user: $DiskUsageReturned{$user}\n");
}

你需要一个keys。否则,您会同时将“用户”和“磁盘使用情况”视为 他们是用户。因为当您在列表上下文中评估哈希时 - 就像您一样 - 返回哈希的每个元素(以随机顺序,但键和值配对)。所以一半的元素将是数字的,而不是对应于哈希中的键,因为它们是一个值。

插图:

#!/usr/bin/perl

use warnings;
use strict;

my %stuff = (
    "me"  => 5,
    "you" => 6,
);
#does the same error, because it tries to look up 
#the value associated with '5' and '6' in the hash. 
foreach my $thing (%stuff) {
    print "$thing\n";
    print "$thing => $stuff{$thing}\n";
}
#doesn't, because "keys %stuff" returns ( "you", "me" )
foreach my $thing ( keys %stuff ) {
    print "$thing => $stuff{$thing}\n";
}

【讨论】:

  • 谢谢,我正在努力理解,所以在这里我在我的代码中进行了这样的测试,但我仍然收到相同的错误消息:foreach my $user (%DiskUsageReturned){ print("$user: $DiskUsageReturned{keys}\n"); }
  • 您需要在哈希上运行keys,以返回用户列表。 foreach my $user ( keys %DiskUsageReturned ) {
  • 谢谢你!您是否碰巧知道为什么我的帖子有 -2 分以及我将来可以做些什么来避免它?
  • @nillenilsson 我认为您的问题被否决了,因为:a)您要求人们为您调试代码,b)您的缩进和格式很差,并且 c)它不是 @ 987654321@(注意“最小”部分)。
猜你喜欢
  • 2017-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-29
  • 1970-01-01
  • 2011-08-01
  • 2019-06-23
相关资源
最近更新 更多