in reply to Better rewrite of multiple ifs

The only way your scheme will work is by turning $name, $pray, etc. into global variables and using the keys of %error_msgs as symbolic references to these globals; i.e.:

for my $var ( keys %error_msgs ) { no strict 'refs'; unless ( $$var ) { print div( { class=>'tip'}, $error_msgs{ $_ } ); $error_found++; } }
(By the way, get in the habit of running perl -c my_program.pl on your code, just to make sure your code at least compiles.)

It is almost certainly not a good idea for you to be doing this (see this series of articles by the Dominus for why you'd want to stay away from symbolic references; and also this). Another (less serious) downside to your scheme is that you lose control over the order in which errors are checked and reported, since it is dictated by the the order in which keys iterates over %error_msgs's hash keys.

A better alternative would be to use parallel hashes (untested):

my @fields = qw( name pray remark email ); my %error_msgs; @error_msgs{ @fields } = ( "Ξέχασες να μας πείς ποιός είσαι!", "Δεν σχολίασες την ευχή!", "Δεν θα μας πείς για την εμπειρία σου?'", "Συμπλήρωσε το email σου!" ); # ...Just one thing, Dude. D'ya have to use s'many cuss words? my %info; # initialize %info for my $field ( @fields ) { unless ( defined $info{ $field } ) { print div( { class => 'tip' }, $error_msgs{ $field } ); $error_found++; } }

the lowliest monk

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.