LPM: H*lp with undef?

David Hempy hempy at ket.org
Thu Nov 18 10:21:28 CST 1999


Okay, I'm a bit stumped here regarding very simple use of a hash.  I want
to say:

	$some_field = $in{"myfield"};				# Line 51
	$foo = "Their field has: " . $in{$some_field};		# Line 52

Problem is, if there is no $in{"myfield"}, then $some_field is undef, and I
get:

	"Use of uninitialized value at mailform.bat line 52."

I do not know in advance whether there will be "myfield" or not...it is
optional.  I had expected the . operator to quietly ignore my attempt to
concatenate a string and undef, as that seems like a pretty perlish sort of
thing from what I've seen.  

I *think* the problem is trying to use . and undef together...I don't think
searching for an undef key in the hash is the problem, but I'm not crystal
clear on that one.



The cleanest solution I've come across so far is:

	$foo = "Their field has: " . $in{$some_field} if defined($some_field);

This still seems pretty cludgey, and not nearly as graceful as the scripts
Rich and Nat share with us.  There are a dozen places I use optional fields
in this CGI script, and would much rather just use them blindly regardless
of whether they are undef or not, instead of testing their values all over
the place.

Can I change the assignment of $some_field so it gets a more benign value
that won't cause errors later on and won't require testing?


Am I missing something here?  Am I trying to push a rope up a hill?  Any
suggestions?


-dave



--
David Hempy
Internet Database Administrator
Kentucky Educational Television




More information about the Lexington-pm mailing list