Hi I'm new to Perl. The reason for the program is part of a procedure to ensure that the configured server IP addrss(obtained from ifconfig) is the same as that contained within the hosts file. The code has been
checked by some more experienced Perl programmers and is shown below. All I want to extract is the first IP address
for fred in the hosts file also shown below
#!/usr/bin/perl
my $HN = `hostname`;
my $hosts =`cat /etc/hosts`;
if ( $hosts =~ /^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\s+(${$HN})\s/g )
{
print "$1\n";
print "$2\n";
}
This program prints the IP address but not the host name ($2) as defined( I thought) by the second set of
parentheses (${$HN})-- Without the ${$HN} NO match is made at all. I know I already have the hostname ($HN)
but dont understand why $2 is not set to $HN
The format of the hosts file(or at least the entries I am interested in) is as follows
x.y.z.22 fred<space or tab>localhost
x.y.z.23 fred-test-0
x.y.z.24 fred-test-1
Can you explain a) why $2 is not populated and b) why just supplying ($HN) instead of (${$HN}) does not work