Hostname in Perl?

I have a few servers whose hostname is "app.prod.cbo.im.foobar.com"

prod is the environment I want to capture in a perl script. I want the script to run on qa and it should be environment independent.So if I am running the script on app.qa.cbo.im.foobar.com I want to capture qa out of that hostname and use that later on in my perl script.

Generally in a shell script I do this -

hostname |awk -F"." '{print $2}'

What would be the perl equivalent of this so that I can put that in a variable.

I did use a2p but got some very verbose results which I did not understand. I want something one liner which I can feed into a variable.

Thanks,
Jack.

How about something like:

$var = `hostname |awk -F"." '{print $2}' `

Yes thats what I use in a shell script but I need a PERL equivalent so I can use in a PERL script.

Thanks,
Jack.

check out Sys::Hostname on CPAN.

On another note PERL should really be spelled Perl.

#!/usr/bin/perl

$var = `hostname`;
@alist = split(/\./, $var) ;
print $alist[1];

---------- Post updated at 07:28 PM ---------- Previous update was at 07:21 PM ----------

Or using Frank's suggestion:

#!/usr/bin/perl
use Sys::Hostname;
$var = hostname;
@alist = split(/\./, $var) ;
print $alist[1];