in reply to gethostbyname one-liner: Improvements?

Folks-

Kudos to kyle++ for a bunch of great ideas, particularly using inet_ntoa() - I had no idea...

Here's the answer to your question on why I need this...

The reason I need this is to run a command on remote machines, which needs the destination IP address (not the host name) in dotted notation, as a command line argument. The destination IP address is different for each remote machine, but is contained in either /etc/hosts, or in DNS, under a common host alias. My current solution is shown is this shell script fragment:
DESTIP=`perl-one-liner("common_host_alias")` remote_command -IP $DESTIP

ikegami++ as always. I've learned something new again (but don't yet understand). I do understand why

inet_ntoa(gethostbyname("www.google.com"))
gives me a usage error, but I don't understand why adding the "". makes it work
inet_ntoa("".gethostbyname("www.google.com"))
Unless this is some new form of black magic, it seems that you're simply adding a null in front of that which is returned by gethostbyname(). Why would that have an effect?

Lastly, jwkrahn++ for pointing out the unix host(1) command. Unfortunately, the box I'm running this on does not have the command installed.

Thanks

-Craig

Replies are listed 'Best First'.
Re^2: gethostbyname one-liner: Improvements?
by ikegami (Patriarch) on Jan 21, 2009 at 16:07 UTC

    it seems that you're simply adding a null in front of that which is returned by gethostbyname(). Why would that have an effect?

    The concat operator imposes a scalar context on its arguments. I used "". as a golfed form for scalar().

Re^2: gethostbyname one-liner: Improvements?
by kyle (Abbot) on Jan 21, 2009 at 15:43 UTC

    The "". in ikegami's solution casts the gethostbyname call as a string, but more importantly it puts it in scalar context instead of the list context it would ordinarily be in as a parameter to inet_ntoa. We want the scalar context because then gethostbyname returns just the packed IP address. In list context it would return more stuff we're not interested in. Using "". here is basically a golfy way to say scalar.