This is acting like a variable scope issue, but I can't see how the hash is scoped to the unless block.
What Should Happen: %app contains several values passed to the script from an HTML post. Each parameter is checked for valid data and, if that data is bad, the value is cleared and an error message is added to the error messages array.
What Does Happen: The value of $app{"first_name"} is not changed to undef in &print_application but @errors is. print statements confirm both are changed inside of the "if (request_method() eq "POST")" block.
Note: The commented line that sets the value to "test_value" works. What gives? Why can't I change the global @errors but not the global %app?
my @errors; my %app; $app{"first_name"} = param('first_name'); # $app{"first_name"} = "test_value"; if (request_method() eq "POST") { my $first_name_check = &check_name($app{"first_name"}); unless ($first_name_check == "1") { $app{"first_name"} = ""; push (@errors, "First Name: ".$first_name_check); } } sub print_application { # do some stuff with %app and @errors... }
In reply to Changing the Value Assigned To A Hash Key by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |