Perl problem (foreach)

Associate
Joined
27 Jun 2006
Posts
1,473
Guys,

Doing a simple little perl script that takes a list of IP addresses from a form textbox and then showing what country they are for. Only problem is the Perl only seems to process the last element of the array!!!

Anyone see what is wrong before I rip the last few tufts of hair out?

Form:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE>IP Country Checker</TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD>

 <BODY>

 <form action="getcountry.pl" method="POST">  
	<textarea cols="15" rows="4" name="ipaddresses"></textarea>
	<input type="submit"><p>
 </form>


 </BODY>
</HTML>
[/FORM]

Perl code is:
Code:
#!/usr/bin/perl

use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use IP::Country::Fast;

$input = param('ipaddresses');
@address = split /\cM\cJ/, $input;

print header(); 
print start_html();

foreach (@address) 
{
	$reg = IP::Country::Fast->new();
	print $_." :: ".$reg->inet_atocc($_)."<br>";
}

print end_html();

Anything obvious???

Cheers!!
 
Hi,

Do you need to declare $myreg with the "my" keyword so it is redeclared for each new object you create. Otherwise I think it might be overwriting the previously created object rather than creating a new instance and leaving the previously created one intact (but referenced by something else).

Been a while since I did any Perl and I'm struggling to remember bits and pieces so the above might not be right. If it isn't let me know.

Hope the above helps though.

Regards,
Jim
 
Cheers for that Jim,

Think it was the way I was testing in the end.
Being stupoid I just dumped in my real IP to check along with a couple of dummy IP addresses (1.1.1.1 & 2.2.2.2).

I thought all IP addresses would have a country and those two didn't so gave me a blank country code back!!!!!

Damn my dozy brain :)

Have slipped the 'my' in there as well anyway.

M.
 
Back
Top Bottom