LPM: H*lp with undef?

Mik Firestone fireston at lexmark.com
Thu Nov 18 10:40:58 CST 1999


$in{' '} = '';			# Init your constant

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

should take care of this problem.  Yes, the purists will likely object.


HTH,
  Mik
On Thu, 18 Nov 1999, David Hempy wrote:

> 
> 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
> 
> 


-- 
Mik Firestone fireston at lexmark.com
When I become an Evil Overlord:
I will be secure in my superiority. Therefore, I will feel no need to prove it
by leaving clues in the form of riddles or leaving my weaker enemies alive to
show they pose no threat.





More information about the Lexington-pm mailing list