slavam has asked for the wisdom of the Perl Monks concerning the following question:
here is the output: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; }
However, should I change subroutine to:perl hostname.pl hostname: not defined ***** correct_UNIX_box_name ****
the output will be:sub getHostname { $hostname = system "/bin/hostname"; print "Hostname: ", $hostname, "\n"; return 0; }
Then in attempt to print output nicely, I made the following changes:perl hostname.pl hostname: not defined ***** correct_UNIX_box_name Hostname: 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:print "Hostname: "; getHostname; sub getHostname { system "/bin/hostname"; return 0; }
and then on the same line:Hostname:
but instead it reveres it and execute subroutine first and then print out line above. here is the output:correct_UNIX_box_name
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.perl hostname.pl hostname: not defined ***** correct_UNIX_box_name Hostname: ****
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 | |
|
Re: not getting hostname correctly on UNIX
by ikegami (Patriarch) on Dec 22, 2005 at 22:51 UTC | |
by blazar (Canon) on Dec 23, 2005 at 11:36 UTC | |
|
Re: not getting hostname correctly on UNIX
by Celada (Monk) on Dec 22, 2005 at 22:54 UTC | |
|
Re: not getting hostname correctly on UNIX
by marto (Cardinal) on Dec 22, 2005 at 23:07 UTC |