Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

not getting hostname correctly on UNIX

by slavam (Initiate)
on Dec 22, 2005 at 22:34 UTC ( [id://518647]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Everyone! Can someone, Please, explain to me kind of weird phenomena. I have perl script below which I’m running on Sun/Solaris
Machine hardware: sun4u OS version: 5.8 Processor type: sparc Hardware: SUNW,Sun-Fire-880 Perl version : This is perl, version 5.005_03 built for sun4-solaris #!/bin/perl #hostname.pl use subs qw(getHostname LoginName HostEntry GetNetByName); my $hostname="not defined"; print "hostname: $hostname\n"; print "*****\n"; getHostname; print "****\n"; sub getHostname { #print "Hostname: "; system "/bin/hostname"; #print "\n"; #$hostname = system "/bin/hostname"; #$hostname = exec "/bin/hostname"; #$hostname = syscall "/bin/hostname"; #print "Hostname: $hostname\n"; return 0; }
here is the output:
perl hostname.pl hostname: not defined ***** correct_UNIX_box_name ****
However, should I change subroutine to:
sub getHostname { $hostname = system "/bin/hostname"; print "Hostname: ", $hostname, "\n"; return 0; }
the output will be:
perl hostname.pl hostname: not defined ***** correct_UNIX_box_name Hostname: 0 ****
Then in attempt to print output nicely, I made the following changes:
print "Hostname: "; getHostname; sub getHostname { system "/bin/hostname"; return 0; }
the output surprised me. I was under impression that Perl is a script language, which means it is being executed line by line… , so it should print out first:
Hostname:
and then on the same line:
correct_UNIX_box_name
but instead it reveres it and execute subroutine first and then print out line above. here is the output:
perl hostname.pl hostname: not defined ***** correct_UNIX_box_name Hostname: ****
I don’t get it… can someone, please explain it? I’m trying to accomplish easy task: print out unix box name I’m currently in. May be someone can suggest another way to get it? The reason I inserted information about OS and UNIX machine is because I suspect it is platform dependant issue. I do not have such problem when I run the same script on Windows XP on PC - everything appears is expected.

Many Thanks in advance.

Replies are listed 'Best First'.
Re: not getting hostname correctly on UNIX
by epoptai (Curate) on Dec 22, 2005 at 23:01 UTC
    Sys::Hostname - Try every conceivable way to get hostname

    --
    perl -MO=Deparse -e"u j t S n a t o e h r , e p l r a h k c r e"

Re: not getting hostname correctly on UNIX
by ikegami (Patriarch) on Dec 22, 2005 at 22:51 UTC

    system doesn't do what you want. You should be using backticks:

    $hostname = `/bin/hostname`;

    Also, you're needlessly using a global for a return value:

    sub getHostname { return `/bin/hostname`; } my $hostname = getHostname; print "hostname: $hostname\n";

      Very valuable advice that the OP would better take into account. But since nobody has mentioned it yet, and he wrote so many lines of code for this simple task, it is also worth pointing out that he may well save himself all this trouble and use Sys::Hostname instead, which I've always found to be quite reliable.

Re: not getting hostname correctly on UNIX
by Celada (Monk) on Dec 22, 2005 at 22:54 UTC

    You can also try getting the hostname without calling an external program:

    perl -MPOSIX -le 'print((uname)[1])'
Re: not getting hostname correctly on UNIX
by marto (Cardinal) on Dec 22, 2005 at 23:07 UTC
    Hi slavam,

    I notice this is your first post (under this username). In addition to the advice given by the other monks, if you have not done so already please read the PerlMonks FAQ and How do I post a question effectively?. Using code tags when posting code make it easier for us to read your posts.

    Hope this helps.

    Martin

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://518647]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 08:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found