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();
}
}
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, 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, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|