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.