Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

gethostbyname: What does $aliases look like?

by Skeeve (Parson)
on Sep 14, 2009 at 06:34 UTC ( #795065=perlquestion: print w/replies, xml ) Need Help??

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 (Pope) 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


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://795065]
Approved by ELISHEVA
Front-paged by tye
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2021-12-03 22:31 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (30 votes). Check out past polls.