Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-24 02:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found