我正在学习Perl,同时我正在为我的家庭事件创建一个程序,但是当我尝试使用随机化过程中的数组时,我收到了一些错误,如您所见:
[[email protected]:~/Desktop/mail] ./get.pl -h pop.vix.terra.com.br -u nathanpc -p (:D)
Global symbol "$random_name" requires explicit package name at ./get.pl line 17.
Execution of ./get.pl aborted due to compilation errors.
[[email protected]:~/Desktop/mail]
我的代码是这样的:
#!/usr/bin/perl
# import packages
use Net::POP3;
use Getopt::Long;
use Mail::Message;
use List::Util qw(shuffle);
use strict;
use warnings;
# Variable declaration
my $host;
my $user;
my $pass;
my $email_file;
my $msg;
my @array = shuffle(<$random_name>);
# read command line options
# display usage message in case of error
GetOptions ('h|host=s' => \$host,
'u|user=s' => \$user,
'p|pass=s' => \$pass) or die("Input error. Try calling me with: -h <host> -u <username> -p <password>");
# file operations
open($email_file, ">>", "Mail.txt");
open my $random_name, "<", "out.txt";
# initiate connection
# default timeout = 120 sec
my $conn = Net::POP3->new($host) or die("ERROR: Unable to connect.\n");
# login
my $numMsg = $conn->login($user, $pass) or die("ERROR: Unable to login.\n");
# get message numbers
# iterate over list and print first 20 lines of each
if ($numMsg > 0) {
my $msgList = $conn->list();
foreach $msg (keys(%$msgList)) {
my $rawdata = $conn->get($msg);
my $msg_obj = Mail::Message->read($rawdata);
my $body = $msg_obj->body;
print $email_file $body;
print $email_file "\n====================================================\n";
print shift @array;
}
} else {
print "Mailbox is empty.\n";
}
# close connection
$conn->quit();
close($email_file);
close($random_name);
您似乎有一些很好的答案,但我希望如果您发布的代码较少,则会有更多的答案。感觉就像你说的那样“我的程序不能正常工作 - 为我调试!”如果您可以将其缩小到一个较小的5行程序(可能更多,取决于问题的复杂程度),以显示您获得的错误,则可能会发现您的问题得到更快速的回答。否则,它可能看起来(对一些人),就像你对你的问题投入很少或没有努力,这往往会使一些人恼火。 – 2009-10-24 02:44:51
对不起,这是因为我要睡觉,我只是贴出来看看我什么时候醒来。对不起 – 2009-10-24 10:46:50
事实证明,你的问题与阵列和随机化无关。我无法弄清楚如何正确地修改这个问题,以便在不删除大量代码的情况下帮助其他人。请自行修改,以便它可以成为其他类似问题的资源。 – 2009-10-24 12:47:48