Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

uninitialized value message? 2

Status
Not open for further replies.

rigstars1

Instructor
Oct 2, 2010
19
US
Hello everyone,

I keep getting this message every so often when my perl script runs. Any one have any ideas how to fix it? The message points to this particular code in my script.

Use of uninitialized value in transliteration (tr///)

my $count = $text =~ tr/://;

Thanks!
 
How about just initialize it as the error message seems clear at least on this point?
[tt]
#somewhere above
my $text[red]=''[/red];
[/tt]
if $text is the variable holding the result of the line read line-by-line of a text file, you've to make sure the loop is properly set up - you've to show it if you're not sure.
 
I've should've of provided more code. I already initialized the variable. Like so ..


sub check_address {

my $text = shift;

# There should only be 1 colon
my $count = $text =~ tr/://;
return undef if $count != 1;

# There should only be three .s
$count = $text =~ tr/.//;
return undef if $count != 3;

my ( $ip, $port ) = split( ':', $text );

# Is the port number sensible
return undef if $port < 1 or $port > 65535;

# Are the address digits sensible
foreach my $x ( split( '\.', $ip ) ) {
return undef if $x < 0 or $x > 255;
}

# All is fine
return 1;
}
 
try this ..

Code:
sub check_address {
 my $text = shift;
 if ($text =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\:(\d{1,5})$/) {
	for ($1,$2,$3,$4) {
 		return undef if $_ < 0 || $_ > 255;
	}
	return undef if $5 <0 || $5 > 65535;
	return 1;
 }
 return undef;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
How do you use check_address as such? Would this do?
[tt]
my $w=check_address('127.0.0.1:80');
if (defined $w) {
print $w."\n";
} else {
print "format erroneous\n";
}
[/tt]
 
Just a supplementary note: if you pass nothing to the sub, it sure would trigger the error as well. Simple remedy would be to stop that in the sub the same manner as in the caller.
[tt]
my $text = shift;
[blue]return undef unless defined $text;[/blue]
# etc... (continue the processing as shown)
[/tt]
Also whether return undef here and elsewhere is perlish enough, I can't say! I just want to keep your style.
 
tsuji.. yeah that is one way to use it but I normally just call it in the if statement like

Code:
use strict;
if (&check_address2('127.0.0.1:123400')) {
	print "Ok\n";
} else {
 print "Not Ok\n";
}

Also if you check the sub it does return bad if you pass no input.. or bad input as it doesn't pass the regex. Not sure if that's what your looking for?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Sure, travis. I was addressing to the op though. Sorry for the confusion.
 
Thanks for all the input. Adding this line of code by tsuji really helped and solved the problem!

return undef unless defined $text;

I'm no longer getting this warning message -

"Use of uninitialized value in transliteration (tr///)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top