Perl

时间:2016-02-23 16:46:07

标签: perl module

我在使特定子程序工作时遇到问题,我有我的模块" module1.pm"我有我的testscript" 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;

在我的模块中使用子程序时,我似乎无法弄清楚我做错了什么。它使用比我输入的更大的主文件夹获取用户,但我也收到很多错误消息。这是我运行&#34; testmodule.pl&#34;。

时得到的输出
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: 

1 个答案:

答案 0 :(得分:3)

你的问题是:

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

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

插图:

#!/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";
}