There are many ways that code could be tidied up, but keeping the logic similar I'd rewrite it as:
#!/usr/bin/perl use warnings; use strict; print <<HEREDOC; Enter: a for lowercase and colons. b to do it the other way around. q to quit HEREDOC while (my $choice = lc <>) { chomp $choice; last if $choice eq 'q'; if ($choice !~ /^[ab]$/) { print "a, b or q expected: "; next; } my $wwn = lc <>; if ($choice eq 'b') { $wwn =~ s/://g; print "$wwn\n"; next; } if (-1 < index $wwn, ':') { print "WWN already contains :\n"; next; } if (16 != length $wwn) { print "WWN must be 16 characters\n"; next; } if ($wwn =~ /^[a-f0-9]/) { print "Non-hex characters are not allowed in WWN\n"; next } print join (':', unpack ("(a2)*", $wwn)), "\n"; }
Note that the nesting has become shallow. Each of the tests is simple and the code associated with them is trivial. Early exits (in this case next and last) are used extensively to avoid the need for else clauses and consequent nesting.
Update: Fixed hex digit match regex - thanks AnomalousMonk
In reply to Re^2: How to insert || and && in an if loop
by GrandFather
in thread How to insert || and && in an if loop
by perl514
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |