You are not logged in.

#1 2011-02-02 23:04:35

hakkum
Member
Registered: 2011-01-09
Posts: 14

Please Critique First PERL Script

Hi all,

I am new to PERL and have just created my first script/program.  It's a random password generator (since there's not enough of those out there as it is).  With this, you can simply run './passgen' and a password will be generated using letters, numbers, and symbols.  Or use the options -L -N -S to suppress the use of letters, numbers, and/or symbols.  Also, the default length is 14, but you can use -l <length> to modify that.

Please let me know what you think. Both about code style and usability.

Thanks in advance!

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 documentation
#               27-jan-2011 (hakkum) program/script created
##############################################################################

=head1 NAME

passgen - generate random alpha-numeric 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 create
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 generation.

=item B<-S>

Prevents the following symbols from being used during the password generation:

` ~ ! @ # $ %
^ & * ( ) _ +
- = \ , . / <
> ? ; ' : " [
] { }

=item B<-p>

Prevents "Password: " from being sent to STDOUT.  This allows for password 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 : "-";
}

EDIT:
  --  changed first line of code from '#!/usr/bin/perl' to '#!/usr/bin/env perl'
  --  changed 'random_password(@ARGV)' to 'random_password()'

-- hakkum

Last edited by hakkum (2011-02-03 06:49:48)


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

Offline

#2 2011-02-03 00:59:23

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,965
Website

Re: Please Critique First PERL Script

It's so... clean. It puts my hackish code to shame. Well done, especially for a first script. smile

I noticed two things:

The clean coding style has made me curious: which language(s) do you normally use?


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#3 2011-02-03 06:43:41

hakkum
Member
Registered: 2011-01-09
Posts: 14

Re: Please Critique First PERL Script

Thank you so much! Coming from someone like you, whom I truly respect, that means a lot!  To be honest this is my first language I've taken the time to really learn.  I've dabbled in Python, PHP, C++, but only enough to grasp the concepts and maybe comprehend very simple scripts.

For the past couple weeks I've been going through 'Learning Perl' and 'Perl Best Practices'.  As of now, I really enjoy the fun of Perl.  I am very OCD so cleanliness is required for me.

I did notice that in my call to random_password() I have the @ARGV argument, which is not needed.  It was there from before, when I didn't use options to get the password length.  It can be removed without worry...thus changing:

print random_password(@ARGV) . "\n";

to

print random_password() . "\n";

Again, thanks Xyne for the comment...solidifies my desire to continue.

--hakkum


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

Offline

#4 2011-02-03 06:45:14

hakkum
Member
Registered: 2011-01-09
Posts: 14

Re: Please Critique First PERL Script

Oh, and BTW, thanks for the heads up on the proper call to Perl...I've made the necessary changes.


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

Offline

Board footer

Powered by FluxBB