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


In reply to Re: Better rewrite of multiple ifs by tlm
in thread Better rewrite of multiple ifs by Nik

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.