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.


In reply to not getting hostname correctly on UNIX by slavam

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.