Hello everyone,

I am new to both Perl and PerlMonks and have created my first script/program. This is my first post to PerkMonks as well. I am looking for a review/critique of the script. I posted it to Arch Linux's forum before discovering this Perl Monastery.

I am actually new to programming in general. I have dabbled a bit in PHP, Python, C++, and a few others, but only enough to comprehend the very basic of concepts. Perl has been quite fun to learn...the reason I've actually stuck with it.

Anyways, below is 'passgen.pl'. It is a random password generator. Of course there's plenty of these floating around, but I needed one and it seemed to be a good starting point. Running the program alone will generate a 14-character password consisting of letters, numbers, and symbols. Passing the -L -N -S options will suppress letters, numbers, and/or symbols accordingly. Also, passing -h will show a short usage summary, showing a few other options.

Please let me know what you think about both code style and usability. Thank you!

passgen.pl

#!/usr/bin/env perl use strict; use warnings; ###################################################################### +######## # # File : passgen.pl # Version : 0.10 # Author : H Bakkum (hakkum) # Email : bakkum.h (at) gmail (dot) com # History : 02-feb-2011 (hakkum) added options and created documen +tation # 27-jan-2011 (hakkum) program/script created # ###################################################################### +######## =head1 NAME passgen - generate random alphanumeric password =head1 SYNOPSIS B<passgen> [-LNSp] [-l I<length>] =head1 DESCRIPTION B<passgen> generates a random password of the specified I<length> and according to the rules specified by the command line options. If no password length is given, a default length of 14 will be generated. + If no options are provided, letters, numbers, and symbols will be used to cr +eate password. =head1 OPTIONS =over 4 =item B<-L> Prevent letters, both uppercase and lowercase, from being used during +the password generation. =item B<-N> Prevents numbers, 0 through 9, from being used during the password gen +eration. =item B<-S> Prevents the following symbols from being used during the password gen +eration: ` ~ ! @ # $ % ^ & * ( ) _ + - = \ , . / < > ? ; ' : " [ ] { } =item B<-p> Prevents "Password: " from being sent to STDOUT. This allows for pass +word to be used in a pipe. =item B<-l> I<length> Specifies the length of the generated password. =item B<-h> Print a summary of options and exit. =back =head1 AUTHOR H Bakkum, <bakkum.h (at) gmail (dot) com> =cut ###################################################################### +######## use Getopt::Std; my %cli_options; getopts("LNSl:ph", \%cli_options); USAGE: { if ($cli_options{h}) { print<<EOF_USAGE; Usage: $0 [OPTION] Generate random password using OPTION rules. -L prevent letters during generation -N prevent numbers during generation -S prevent symbols during generation -l length override default password length -p allow password to be piped -h display this help and exit Created by H Bakkum (hakkum) Report bugs to <bakkum.h (at) gmail (dot) com> EOF_USAGE exit 0; } } MAIN: { print "Password: " if not $cli_options{p}; print random_password() . "\n"; } #--------------------------------------------------------------------- +-------- # &random_password() #--------------------------------------------------------------------- +-------- sub random_password { # define variables my @chars_pool; my $pass_len; my $password; my $random_number; # characters in password @chars_pool = character_pool(); # default password length $pass_len = 14; # if command line argument, use as password length if ($cli_options{l}) { $pass_len = $cli_options{l} if $cli_options{l} =~ /^\d+$/; $pass_len = 14 if $pass_len <= 0; $pass_len = 100 if $pass_len >= 100; } # generate password for (1..$pass_len) { $random_number = int rand @chars_pool; $password .= $chars_pool[$random_number]; } $password; } #--------------------------------------------------------------------- +-------- # &character_pool() #--------------------------------------------------------------------- +-------- sub character_pool { # define variables my @strings; my @digits; my @symbols; my @chars_pool; @strings = ('a'..'z', 'A'..'Z'); @digits = (0..9); @symbols = ( '`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '-', '=', '\\', ',', '.', '/', '<', '>', '?', ';', '\'', ':', '"', '[', ']', '{', '}', ); push @chars_pool, @strings if not $cli_options{L}; push @chars_pool, @digits if not $cli_options{N}; push @chars_pool, @symbols if not $cli_options{S}; @chars_pool ? @chars_pool : "-"; }

-- hakkum

...never forget the hakkum bakkum,
the hakkum bakkum never forgets...

In reply to Please Review First Program: Random Password Generator by hakkum

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.