in reply to genpass Password Generator

Heredocs are a nice way to tidy up help messages and such. Consider:

sub DisplayUsage { print <<" USAGE"; Usage: genpass [-OPTIONS] LENGTH Generate secure passwords LENGTH characters long. -s, --symbols\t\tExclude symbol characters. -n, --numbers\t\tExclude number characters. -u, --uppercase\t\tExclude uppercase characters. -l, --lowercase\t\tExclude lowercase characters. -q(X)\t\t\tCreate X number of passwords. --help\t\t\tDisplay this usage screen. Report bugs, comments, and questions to jbrown_home\@yahoo.ca USAGE }

Note also that the prototype has been omitted from the sub. Apart from exceptional cases, prototypes are not useful and tend to lead to subtle and hard to find bugs.

Note too that the white space is an important part of the here document tag. There is a nasty and subtle trap here - the white space must match exactly for the end of the document to be recognised.

I'd be inclined to rewrite the password generation sub a little: ;)

sub GenPass { my ($pwdlen, %flags) = @_; my $password=""; my $num; while (length ($password) < $pwdlen) { $num = GetRandNum (); if (($num >= 33 && $num <= 47) || ($num >= 58 && $num <= 64) | +| ($num >= 91 && $num <= 95) || ($num >= 123 && $num <= 126) ) { next if $flags{symbols} eq '0'; } if (($num >= 48 && $num <= 57)) { next if $flags{numbers} eq '0'; } if (($num >= 65 && $num <= 90)) { next if $flags{uppercase} eq '0'; } if (($num >= 97 && $num <= 122)) { next if $flags{lowercase} eq '0'; } redo; # Invalid character, try again } continue { $password .= chr($num); } return "$password\n"; }

Main points are:


DWIM is Perl's answer to Gödel