469,270 Members | 1,117 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

Get full computer name using Perl

7
I want to get the full computer name using the ActiveState Perl on windows .
Actually i can get that using the "hostname" command, but i want to have the output of the "hostname" command stored in some variable using perl. And i wan to use that variable in my script.

Is it possibel to write a script which will store the return value of the command executed on the command prompt and how ???

Kindly help
May 10 '07 #1
6 28405
MMcCarthy
14,534 Expert Mod 8TB
You have posted this in the Articles section. I am moving it to the Perl forum.

ADMIN
May 10 '07 #2
KevinADC
4,059 Expert 2GB
I don't understand your question well enough to answer it. You can store return values various ways: in variables or in a database or in a text file. If the value is only needed for the life of the script store it in a variable, if the value needs persistence, store it in a file or database or look into the Storable module, which comes with perl.
May 10 '07 #3
prn
254 Expert 100+
I'm guessing that what ajays means is that he doesn't know how to capture the information that "hostname" outputs. OTOH, I might also note that "hostname" does not reliably put out an FQDN (fully qualified domain name). Assuming that ajays is using the Active State Perl (that's by far the most common Perl for windows), he should have the Net:: Domain module available. See http://www.xav.com/perl/site/lib/Net/Domain.html for specs. This may prove more reliable than just "hostname".

Try:
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use Net::Domain qw (hostname hostfqdn hostdomain);
  3.  
  4. my $hostname = `hostname`;
  5. print "my hostname is $hostname\n";
  6.  
  7. my $fqdn = hostfqdn();
  8. print "my fqdn is $fqdn\n";
  9.  
and see what you get. On my windows box, I get:

Expand|Select|Wrap|Line Numbers
  1. my hostname is phobos
  2. my fqdn is phobos.bsu.edu
  3.  
HTH,
Paul
May 11 '07 #4
KevinADC
4,059 Expert 2GB
You're probably right Paul. Hopefully he comes back and reads your reply.
May 11 '07 #5
HeinV
1
I stumbled into this topic years later.
Just want to point out that prn's example appears to work, but does not do that it suggests.

prn>> use Net::Domain qw (hostname hostfqdn hostdomain);
prn>> my $hostname = `hostname`;

The first line 'activates' the hostname function,
But the second name executes (back-tick / qx) a command using the string 'hostname'... which typically gives the same result, and does appear to work, but less reliably so.
Just drop the back-ticks and it will use the function, not a fresh process.

Cheers,
Hein
Oct 31 '13 #6
RonB
589 Expert Mod 512MB
Hein,

I believe you misinterpreted what Paul was demonstrating.

The OP was asking about how to capture the output of the system's hostname command. Paul was demonstrating the difference between doing that and using the hostfqdn() function from the module. It would have been more clear if he didn't include/import the hostname() and hostdomain() functions in the use statement.

Here's an updated (corrected?) version.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use warnings;
  4. use strict;
  5. use Net::Domain qw(hostname hostfqdn);
  6.  
  7. my $hostname = `hostname`;
  8. print "my hostname is $hostname\n";
  9.  
  10. my $fqdn_hostname = hostname();
  11. print qq(my "fqdn" hostname is $fqdn_hostname\n\n);
  12.  
  13. my $fqdn = hostfqdn();
  14. print "my fqdn is $fqdn\n";
Oct 31 '13 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Johnson Bruno | last post: by
3 posts views Thread by Xah Lee | last post: by
7 posts views Thread by Jason Reichenbach | last post: by
reply views Thread by Chung Leong | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.