On Fri, Dec 22, 2006 at 04:21:42PM +0000, George Geller wrote:
> This perl script works, but is not a idomatic perl.
> How would a perl expert write it?

I've been called a crazed Perl hacker.  That's the same thing, right?

> This is my second perl script that will actually be used by somebody else.

This is how I would write it, hope it helps:

#!/usr/bin/perl
#
# Send the out the IP address of eth0 to stdout.

use strict;
use warnings;

my $ifcfg = `/sbin/ifconfig eth0`;

# example:
# eth0      Link encap:Ethernet  HWaddr 00:14:2A:99:DF:2F
#           inet addr:192.168.2.12  Bcast:192.168.2.255
#           Mask:255.255.255.0
#           inet6 addr: fe80::214:2aff:fe99:df2f/64 Scope:Link
#           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
#           RX packets:43306 errors:0 dropped:0 overruns:0 frame:0
#           TX packets:64326 errors:0 dropped:0 overruns:0 carrier:0
#           collisions:0 txqueuelen:1000
#           RX bytes:6378858 (6.0 MiB)  TX bytes:58482280 (55.7 MiB)
#           Interrupt:193 Base address:0xe000

if ( my ($ip) = $ifcfg =~ /inet[ 
]addr:([0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3})/xms ) {
    print "$ip\n";
}
else {
    die "No match.\n";
}

Attachment: pgptfq08MqVf0.pgp
Description: PGP signature

-- 
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-lpsg

Reply via email to