If
$val2 is a regular scalar, then
ref($val2) will return
undef since it is not
blessed. Makes perfect sense, right?
I'm not sure why you have a
ref call in there.
You might also have made a mistake in your first loop.
$val declared in your loop,
$val2 used in your print. You should have got a warning.