Skip to content

Commit

Permalink
refactoring _get_interface_info
Browse files Browse the repository at this point in the history
  • Loading branch information
xsawyerx committed Sep 15, 2010
1 parent 43bec3a commit 8f162c9
Showing 1 changed file with 42 additions and 31 deletions.
73 changes: 42 additions & 31 deletions lib/Sys/HostIP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,56 @@ sub ifconfig {
}

sub ip {
my $self = shift || 'Sys::HostIP';
return $self->_get_interface_info( mode => 'ip' );
my $self = shift || 'Sys::HostIP';
my $if_info = ref $self ?
$self->if_info :
$self->_get_interface_info;

if ( $^O =~/(MSWin32|cygwin)/ ) {
foreach my $key ( sort keys %{$if_info} ) {
# should this be the default?
if ( $key =~ /Local Area Connection/ ) {
return ( $if_info->{$key} );
}
}
} else {
foreach my $key ( sort keys %{$if_info} ) {
# we don't want the loopback
next if ( $if_info->{$key} eq '127.0.0.1' );
# now we return the first one that comes up
return ( $if_info->{$key} );
}

# we get here if loopback is the only active device
return '127.0.0.1';
}
}

sub ips {
my $self = shift || 'Sys::HostIP';
return $self->_get_interface_info( mode => 'ips' );

if ( ref $self ) {
return [ values %{ $self->if_info } ];
} else {
return [ values %{ $self->_get_interface_info } ];
}
}

sub interfaces {
my $self = shift || 'Sys::HostIP';
return $self->_get_interface_info( mode => 'interfaces' );

if ( ref $self ) {
return $self->if_info;
} else {
return $self->_get_interface_info;
}
}

sub if_info {
my $self = shift;
$self->{'if_info'} or $self->{'if_info'} = $self->_get_interface_info;

return $self->{'if_info'};
}

sub _get_interface_info {
Expand All @@ -77,33 +115,6 @@ sub _get_interface_info {
} else {
$if_info = $self->_get_unix_interface_info();
}

if ($params{mode} eq 'interfaces') {
return $if_info;

} elsif ( $params{mode} eq 'ips') {
return [values %$if_info];

} elsif ( $params{mode} eq 'ip') {
if ($^O =~/(MSWin32|cygwin)/) {
foreach my $key (sort keys %$if_info) {
# should this be the default?
if ($key=~/Local Area Connection/) {
return ($if_info->{$key});
}
}
} else {
foreach my $key (sort keys %$if_info) {
# we don't want the loopback
next if ($if_info->{$key} eq '127.0.0.1');
# now we return the first one that comes up
return ($if_info->{$key});
}

# we get here if loopback is the only active device
return "127.0.0.1";
}
}
}

sub _get_unix_interface_info {
Expand Down

0 comments on commit 8f162c9

Please sign in to comment.