全局符号“$ line”和“$ addr”需要显式包名

时间:2013-11-18 15:57:55

标签: perl ip-address

我正在尝试让输出显示ip地址列表并使用相应的国家/地区登录,但我不断收到这些错误:全局符号$ line和$ addr需要显式包名称。它在Perl中工作正常,但我从服务器运行此脚本。有人有主意吗?感谢。

#!/usr/bin/perl

my $psql = "/usr/local/pgsql/current/bin/psql";
my $db = 'cpi';
my $args = "-U postgres -qc";

my $date = `/bin/date +\%y\%m\%d%H`;
my $reportfile = "/tmp/multiiplogins-$date";

my $sendmail = "/usr/sbin/sendmail -t -fcpi\@cpi-syndication.com";
my $mailsubject = "Login Report";
my $mailto = 'user@yahoo.com';

my $query = "SELECT userid, login, email, logins, ips FROM (SELECT userid,login,email,       count(userid) AS logins, count(ipaddr) AS ips FROM (SELECT l.userid, u.login, u.email$

my $query2 = "SELECT l.userid, login, email, ipaddr FROM synloginaccess l, synusers u where l.accesstime > (now() - interval '24 hours') and l.type=2 and l.userid=u.userid $

open (REPORT, ">$reportfile");

my $command = qq/$psql $db $args "$query"/;
my $command2 = qq/$psql $db $args "$query2"/;

my $result = `$command`;
my $result2 = `$command2`;

#update IP addresses with country
use strict;
use warnings;
use Net::IPInfoDB;

my $g = Net::IPInfoDB->new;
$g->key("api_key");

#we split $login into an array, line-by-line
my @lines = split("\n",$result2);
for my $line (@lines) {
   #now we iterate through every line one-by-one
   $line =~ /(?<ip>\d+\.\d+\.\d+\.\d+)/;
   my $addr = $g->get_country("$1");
   print "$line " . "| ". "$addr->country_name" ."\n";

 }


    #print REPORT "$result2\n";
    #print REPORT "\n";
    print REPORT "$line " . "| ". "$addr->country_name" ."\n";

  close REPORT;
  mailReport();

 sub mailReport{
     #mail it
     open(MAIL, "|$sendmail");
     print MAIL "To: $mailto\n";
     print MAIL "Subject: $mailsubject\n";
     print MAIL "\n";
     open (INFILE, "$reportfile");
     my @contents = <INFILE>;

1 个答案:

答案 0 :(得分:3)

当您尝试在for循环后打印它们时,$line$addr变量不再在范围内:

#print REPORT "$result2\n";
#print REPORT "\n";
print REPORT "$line " . "| ". "$addr->country_name" ."\n";

我想这条线应该被注释掉。