http://qs321.pair.com?node_id=11118334

x_stream3m has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, learning Perl for the first time. I am trying to see if there is a more efficient way of going about verifying the contents of a scalar value.

I have a password input. If the password length is between:

- 8 and 11, the password should contain a-zA-Z0-9 and special characters.

- 12 and 15, the password should contain a-zA-Z0-9.

- 16 and 19, the password should contain a-zA-Z

- 20 and above, no criteria

I feel the current implementation is kind of redundant or inefficient? Maybe I am wrong?

my $password = <STDIN>; my $lengthOfPassword = length $password; given ($lengthOfPassword) { when ($_ >= 8 && $_ <= 11) { if ($password =~ /[a-z]/ && $password =~ /[A-Z]/ && $password =~ /[0-9]/ && ($password =~ /[\x21-\x2F]/ || $password =~ /[\x3A-\x40]/ || $password =~ /[\x5B-\x60]/ || $password =~ /[\x7B-\x7E]/)) { say "Successful password"; } else { invalidPasswordError(); } } when ($_ >= 12 && $_ <= 15) { if ($password =~ /[a-z]/ && $password =~ /[A-Z]/ && $password =~ /[0-9]/) { say "Successful password"; } else { invalidPasswordError(); } } when ($_ >= 16 && $_ <= 19) { if ($password =~ /[a-z]/ && $password =~ /[A-Z]/) { say "Successful password"; } else { invalidPasswordError(); } } when ($_ >= 20) { say "Successful password"; } default { lengthPasswordError(); } }