Operating System - HP-UX
1835245 Members
2206 Online
110078 Solutions
New Discussion

Use of uninitialized value $_ in split

 
SOLVED
Go to solution
Allanm
Super Advisor

Use of uninitialized value $_ in split

Hi!

I have created the attached script to check to see if a connection to a port can be made.
Basically the script runs like this -

script1.pl -f filename.conf

where filename.conf has the following content -

host:port

but when I am running the script I am getting "Use of uninitialized value $_ in split".

Can you help here?

Thanks,
Allan
4 REPLIES 4
TwoProc
Honored Contributor
Solution

Re: Use of uninitialized value $_ in split

I'm thinking that you can't use $_ with pulling data from an array. You opened the file, but you didn't read it into any structure for processing.

----------------------
Yours:
open (FILE, $filename) or die "Unable to open $filename";
until (eof FILE){

$FS = ':';
($Fld1,$Fld2) = split($FS, $_);
my $hostname = $Fld1;
my $portnumber = $Fld2;
}
close (FILE);

-----------------
Untested, but I suggest something like the following, which always seems to get $_ working with a foreach loop.


open (FILE, $filename) or die "Unable to open $filename";
@array=;
close (FILE);

$FS = ':';
foreach (@array) {
($hostname,$portnumber) = split($FS, $_);
}
We are the people our parents warned us about --Jimmy Buffett
James R. Ferguson
Acclaimed Contributor

Re: Use of uninitialized value $_ in split

Hi:

Allan :

You never read the configuration file, and hence '$_' is undefined. Try:

#!/usr/bin/perl
use strict;
use warnings;
use Socket;
use Getopt::Std;
# set time until connection attempt times out
my $timeout = 3;
if ($#ARGV != 1) {
print "usage: script.pl -f filename.conf \n";
exit 2;
}

our $opt_f;
getopts("f:");
my $filename = $opt_f;

open (FILE, $filename) or die "Unable to open $filename";
my ($hostname, $portnumber) = split(/:/, );

close (FILE);

my $host = shift || $hostname;
my $port = shift || $portnumber;
my $proto = getprotobyname('tcp');
my $iaddr = inet_aton($host);
my $paddr = sockaddr_in($port, $iaddr);

socket(SOCKET, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
# socket opened

eval {
local $SIG{ALRM} = sub { die "timeout" };
alarm($timeout);
connect(SOCKET, $paddr) || error();
alarm(0);
};
if ($@) {
close SOCKET || die "close: $!";
print "$hostname is NOT listening on tcp port $portnumber.\n";
exit 1;
}
else {
close SOCKET || die "close: $!";
print "$hostname is listening on tcp port $portnumber.\n";
exit 0;
}

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Use of uninitialized value $_ in split

Hi Allan:

You have a large number of questions with unassigned points (159 of 278 responses). It would be appreciated if you could evaluate the help you received.

http://forums.itrc.hp.com/service/forums/pageList.do?userId=CA1505975&listType=unassigned&forumId=1

Regards!

...JRF...
Allanm
Super Advisor

Re: Use of uninitialized value $_ in split

Sorry I have provided the points for this thread. WIll work on some of the older ones now.

Thanks,
Allan