Skeeve has asked for the wisdom of the Perl Monks concerning the following question:

I wrote a small script where I use

($name, $aliases, $addrtype, $length, @addrs) = gethostbyname $host;
and since I don't have any host having more than 2 names (i.e. 1 alias) I don't know what $aliases would look like, had the machine 3 or more names.

Are the names in a string? Space seperated? Komma seperated?

Or wil $aliases be a reference to an array?

Can someone please enlighten me?


Replies are listed 'Best First'.
Re: gethostbyname: What does $aliases look like?
by tomfahle (Priest) on Sep 14, 2009 at 07:32 UTC

    Just add some aliases where it doesn't hurt and it's easy to figure out. (I use a tiny virtual machine for this purpose.)

    #!/usr/bin/perl use strict; use warnings; use Dumpvalue; # Core Module, dumps like the debugger my $dumper = new Dumpvalue; my $host = shift; my ($name,$aliases,$addrtype,$length,@addrs) = gethostbyname($host) or + die "Can't resolve $host $!\n"; print "$aliases\n"; $dumper->dumpValue(\$aliases);

    My /etc/hosts file localhost tf-laptop my-laptop

    Running the above code with the argument localhost yields:

    $ perl localhost tf-laptop my-laptop -> 'tf-laptop my-laptop'

    $aliases is a string holding the aliases seperated by whitespace.


Re: gethostbyname: What does $aliases look like?
by ikegami (Patriarch) on Sep 14, 2009 at 06:58 UTC

    An array of strings, I would suppose, like the system call.

    Then again, Perl doesn't return an array when there's only one alias. Maybe it returns one at most?

    $ perl -MData::Dumper -e'print Dumper $_ for (gethostbyname $ARGV[0])[ +0,1]' $VAR1 = ''; $VAR1 = '';

    Update: They are space-joined. From pp_sys.c:

      Thanks, ikegami