As per advice given to me in Find Ethernet Card Manufacturer, I've created my first real module. My module mirrors a copy of a remote file the location specified in my module's variable $oui_location. I would like the script that uses my module to be able to change that value.
I tried including $oui_location in the @EXPORT_OK array and then doing something like this in the calling script:
use OUILookup ('get_oui_owner', 'oui_location');
$oui_location = "oui2.txt";
but it didn't work.
What do I have to do?
package OUILookup;
require Exporter;
use LWP::Simple;
use strict;
use warnings;
our @ISA = qw(Exporter);
our @EXPORT = qw(get_oui_owner);
our $VERSION = 1.00;
my $oui_address = "http://standards.ieee.org/regauth/oui/oui.txt";
my $oui_location = "oui.txt";
sub get_oui_owner {
my ($mac) = @_;
my $oui = validate_input($mac);
my $oui_entry;
my $foundit = 0;
if ($oui eq "invalid address format") {
return $oui;
}
else {
if (update_ouidb()) {
open(OUI, $oui_location) or die "Unable to locate $oui_loc
+ation: $!\n";
while (<OUI>) {
chomp;
if (/$oui/) {
$oui_entry = $_;
$foundit = 1;
last;
}
}
close(OUI);
if (!$foundit) {
return "OUI not found";
}
else {
$oui_entry =~ s/\w{2}-\w{2}-\w{2}|^\w{6}//g;
$oui_entry =~ s/\(hex\)|\(base 16\)//g;
$oui_entry =~ s/^\s+//g;
return $oui_entry;
}
}
}
}
sub update_ouidb {
my $errcode = mirror("http://standards.ieee.org/regauth/oui/oui.tx
+t", $oui_location);
if (!($errcode eq "200" || "304")) {
die "problem updating OUI database: $!\n";
}
else {
return 1;
}
}
sub validate_input {
my ($input) = @_;
my @bytes;
my $oui;
if ($input =~ /\w{2}-\w{2}-\w{2}-\w{2}-\w{2}-\w{2}/) {
@bytes = split '-', $input;
$oui = join '-', $bytes[0], $bytes[1], $bytes[2];
return $oui;
}
else {
return "invalid address format";
}
}
1;
=head1 NAME
OUILookup
=head1 SYNOPSIS
Find the owner of the IEEE Organizationally Unique Identifier (OUI)
for a particular MAC address.
OUILookup uses the IEEE database from <http://standards.ieee.org/regau
+th/oui/oui.txt>.
It will create a local copy of that database if one does not already e
+xist, and update
a the local copy if it does exist.
By default, the local copy will be stored as oui.txt in the same direc
+tory as the script.
This location is set by the module variable $oui_location.
=head1 USAGE
use OUILookup;
$mac = "00-10-5A-1B-C3-30";
$owner = get_oui_owner($mac);
=head1 REQUIRES
LWP::Simple
=head1 TESTED
Perl 5.6.0 on Windows NT (Service Pack 6)
=head1 AUTHOR
Micah Valine <mvaline@buffnet.net>
=cut