Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jplindstrom (594)

jplindstrom
  (email not shown publicly)

Journal of jplindstrom (594)

Wednesday May 22, 2002
05:01 PM

hostname

[ #5161 ]

Did you know that there is a hostname.exe on wk2?

That's very useful, knowing there's a cross platform (for limited values of platform) command for identifying the machine.

From now on all my alert e-mails should state where they originate.

Quiz: is there perhaps a module providing the equivalent of `hostname`?

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Sys::Hostname [uwinnipeg.ca] should do the trick (and it is core):
    Attempts several methods of getting the system hostname and then caches the result. It tries the first available of the C library's gethostname(), `$Config{aphostname}`, uname(2), syscall(SYS_gethostname), `hostname`, `uname -n`, and the file /com/host
    • Re:yes (Score:2, Informative)

      Unfortunately it doesn't include the domain name on Windows:

      perl -MSys::Hostname -e"print hostname"
      foo

      POSIX::uname works better:

      perl -MPOSIX -e"print ((uname)[1])"
      foo.activestate.com
      • Is it worth patching Sys::Hostname to a less broken behaviour then?
        • I would think so. The fix would be to try uname before gethostname, at least on Windows. Currently, gethostname succeeds, returning the domainless hostname and uname is never given a chance to show what it knows. :)
          • Patch sent to author. I also patched Sys::Hostname::Long which is completely broken on win32.
            • Gah. Email address of the author of Sys::Hostname isn't valid. Not sure what to do, so here is the fix anyway:

              --- Hostname-old.pm Thu May 23 16:02:04 2002
              +++ Hostname.pm Thu May 23 16:09:41 2002
              @@ -51,7 +51,12 @@
               
                    }
                    elsif ($^O eq 'MSWin32') {
              - ($host) = gethostbyname('localhost');
              + eval {
              + local $SIG{__DIE__};
              + require POSIX;
              + $host = (POSIX::uname())[1];
              + };
              + ($host) = gethostbyname('localhost') u
              • No, it has to be fixed in the XS code for ghname(). All the Perl code is only a fallback if the XS code doesn't find a hostname.

                Sys::Hostname is a core module, so patches should go to p5p.