George Geller wrote:
> This perl script works, but is not a idomatic perl.
> How would a perl expert write it?
> This is my second perl script that will actually be used by somebody else.
I agree with Lan for the most part. I would remove things like the use
of $_ and combine the $x assignments, and make the RE clearer.
Of course, in a one-liner, I would use something like this:
% /sbin/ifconfig eth0| perl -ne 'm/inet addr:([^ ]*)/ && print "$1\n"'
172.19.1.106
That RE is a lot easier to read, IMHO than trying to match an exact IP
address. This is what I came up with:
#!/usr/bin/perl -T -w
$ENV{PATH}="";
$ifc=qx(/sbin/ifconfig eth0);
if ($ifc =~ m/inet addr:([^ ]*)/) {
print "$1\n";
} else {
print "No match.\n"
}
exit 0;
__END__
Comment to suit your needs.
Prints all the IP addresses (including loopback, I am afraid):
#!/usr/bin/perl -T -w
$ENV{PATH}="";
for (qx(/sbin/ifconfig -a)) {
m/inet addr:([^ ]*)/ && print "$1\n";
}
exit 0;
-john
--
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-lpsg