S
soren625
I'm using Getopt::Std, and I made a little test script to confirm that
when an option is supposed to have a parameter and is passed without
the parameter, it is completely ignored (i.e. not even pushed to the
options hash).
This is what I want:
prompt> rand.pl -f
runs script.pl and saves the output to a file at the *default* path
<OR>
prompt> rand.pl -f /path/to/file
runs the script and saves the output to the *specified* path.
This is what I have (I'm including the whole script because I'd also
like critique on style/efficiency/etc.):
#!/usr/bin/perl -w
# rand.pl is a random string generator. I created it specifically for
use with the KeePass password management
# application. When generating passwords with KeePass, the user is
asked to type random keyboard input, "...the
# more the better. The different, the better. The random the better"
[sic]. Since a large random string
# was needed immediately, I wanted a one-off commandline solution that
would stick the generated string
# in the clipboard for pasting into KeePass. KeePass allows a maximum
of 30,000 characters in the text box
# so that's the default output. I thought rand.pl could be used for
other applications, so I added the ability to
# specify the string length (indeed, rand.pl itself could be used as a
simple password generator) and the option
# to write the string to a file.
#
# To Do:
# -- when the -f option is used alone, the default path should be used,
# optionally, -f [filename] may be used to specify save location. This
will eliminate the need for further
# prompting after the initial command is given
# -- add option to select character classes to include or exclude
(alpha caps, numbers, brackets, whitespace, etc.)
# -- add option to include or exclude specific characters
#
use Env; # This module allows us to use system environment variables
use Getopt::Std; # This module allows us to grab commandline options
use Win32::Clipboard; # This module allows us to manipulate the Windows
clipboard
use strict;
my %opt;
my $random_string;
my $homepath = $ENV{HOME};
my $chars = 30000; # We initialize this one with a default value
(in case there are no commandline options)
my $file;
my @charlist = (0 .. 9, 'A' .. 'Z', 'a' .. 'z', '!', '@', '#', '$',
'%', '^',
'&', '*', '(', ')', '-', '_', '=', '+', '\\', '|', '[', ']',
# The list of characters used
'{', '}', '`', '~', ';', ':', '\'', '"', ',', '<', '.', '>',
# to build the random string
my $opt_string = 'l:fch';
getopts( "$opt_string", \%opt );
if($opt{h}){
&print_help;
die("\n");
}
if($opt{l}){
$chars = $opt{l}
}
if($opt{c}){
&generate;
&clip_out;
die("\n\n*** Random string of $chars characters generated and placed
in the clipboard. ***\n");
}
if($opt{f}){
&generate;
&file_out;
die("\n\n*** Random string of $chars characters generated and written
to $file. ***\n");
}
else{
&generate;
&clip_out;
die("\n\n*** Random string of $chars characters generated and placed
in the clipboard. ***\n");
}
#
# *** BEGIN SUBROUTINES ***
#
#
# Generate the random string with length of the variable $chars
#
sub generate{
$random_string = join "" => map $charlist[rand @charlist] => 1 ..
$chars;
return;
}
#
# Use the Win32::Clipboard module to dump the generated string to the
clipboard.
#
sub clip_out{
Win32::Clipboard::Set($random_string);
return;
}
#
# Output to a file
#
sub file_out{
print("Type the path and name of the file\n");
print("(Use UNIX-style forward slashes and DOS-style\n");
print("8 character names -- example: c:/docume~1/dir/file.txt\n");
print("Default is \"random.txt\" saved on your Windows desktop) >>");
$file = <STDIN>;
chomp $file;
if($file eq ""){
$file = "$homepath\\desktop\\random.txt";
}
open(OUTFILE, "> $file") or die "Could not access output file $file:
$!\n";
print(OUTFILE $random_string);
close(OUTFILE);
return;
}
#
# Print the help statement
#
sub print_help{
print <<"END";
*********************************************************************
Help for the Random String Generator
Usage: perl rand.pl -l [length] -f
perl rand.pl -l [length] -c
rand.pl is a Perl script that generates a random string
and writes it to a file or places it on the Windows clipboard
to be pasted later. If no command line options are specified,
a 30,000 character string is placed in the clipboard.
Command line options:
-h Get help using randgen.pl (this file)
-l [length] Set the length of the generated string
(default is 30000)
-c Place the generated string in the Windows clipboard
-f Write the generated string to a file (you'll be
prompted for a path -- default is
c:/docume~1/\%HOME%/desktop/random.txt (your home
directory))
*********************************************************************
END
return;
}
As you can see, if the -f option is passed, I am prompting for save
path in the file_out subroutine. I'd like to do away with that and only
have to run one command and not have to interact with the program
anymore.
So, if I have told perl that f will have a parameter (by adding the
colon in the getopts function), is it true that the -f option gets
completely ignored if there is no parameter passed with it from the
commandline?
How can I check to see if the -f option was passed without a parameter
then?
Thanks for your help.
when an option is supposed to have a parameter and is passed without
the parameter, it is completely ignored (i.e. not even pushed to the
options hash).
This is what I want:
prompt> rand.pl -f
runs script.pl and saves the output to a file at the *default* path
<OR>
prompt> rand.pl -f /path/to/file
runs the script and saves the output to the *specified* path.
This is what I have (I'm including the whole script because I'd also
like critique on style/efficiency/etc.):
#!/usr/bin/perl -w
# rand.pl is a random string generator. I created it specifically for
use with the KeePass password management
# application. When generating passwords with KeePass, the user is
asked to type random keyboard input, "...the
# more the better. The different, the better. The random the better"
[sic]. Since a large random string
# was needed immediately, I wanted a one-off commandline solution that
would stick the generated string
# in the clipboard for pasting into KeePass. KeePass allows a maximum
of 30,000 characters in the text box
# so that's the default output. I thought rand.pl could be used for
other applications, so I added the ability to
# specify the string length (indeed, rand.pl itself could be used as a
simple password generator) and the option
# to write the string to a file.
#
# To Do:
# -- when the -f option is used alone, the default path should be used,
# optionally, -f [filename] may be used to specify save location. This
will eliminate the need for further
# prompting after the initial command is given
# -- add option to select character classes to include or exclude
(alpha caps, numbers, brackets, whitespace, etc.)
# -- add option to include or exclude specific characters
#
use Env; # This module allows us to use system environment variables
use Getopt::Std; # This module allows us to grab commandline options
use Win32::Clipboard; # This module allows us to manipulate the Windows
clipboard
use strict;
my %opt;
my $random_string;
my $homepath = $ENV{HOME};
my $chars = 30000; # We initialize this one with a default value
(in case there are no commandline options)
my $file;
my @charlist = (0 .. 9, 'A' .. 'Z', 'a' .. 'z', '!', '@', '#', '$',
'%', '^',
'&', '*', '(', ')', '-', '_', '=', '+', '\\', '|', '[', ']',
# The list of characters used
'{', '}', '`', '~', ';', ':', '\'', '"', ',', '<', '.', '>',
# to build the random string
my $opt_string = 'l:fch';
getopts( "$opt_string", \%opt );
if($opt{h}){
&print_help;
die("\n");
}
if($opt{l}){
$chars = $opt{l}
}
if($opt{c}){
&generate;
&clip_out;
die("\n\n*** Random string of $chars characters generated and placed
in the clipboard. ***\n");
}
if($opt{f}){
&generate;
&file_out;
die("\n\n*** Random string of $chars characters generated and written
to $file. ***\n");
}
else{
&generate;
&clip_out;
die("\n\n*** Random string of $chars characters generated and placed
in the clipboard. ***\n");
}
#
# *** BEGIN SUBROUTINES ***
#
#
# Generate the random string with length of the variable $chars
#
sub generate{
$random_string = join "" => map $charlist[rand @charlist] => 1 ..
$chars;
return;
}
#
# Use the Win32::Clipboard module to dump the generated string to the
clipboard.
#
sub clip_out{
Win32::Clipboard::Set($random_string);
return;
}
#
# Output to a file
#
sub file_out{
print("Type the path and name of the file\n");
print("(Use UNIX-style forward slashes and DOS-style\n");
print("8 character names -- example: c:/docume~1/dir/file.txt\n");
print("Default is \"random.txt\" saved on your Windows desktop) >>");
$file = <STDIN>;
chomp $file;
if($file eq ""){
$file = "$homepath\\desktop\\random.txt";
}
open(OUTFILE, "> $file") or die "Could not access output file $file:
$!\n";
print(OUTFILE $random_string);
close(OUTFILE);
return;
}
#
# Print the help statement
#
sub print_help{
print <<"END";
*********************************************************************
Help for the Random String Generator
Usage: perl rand.pl -l [length] -f
perl rand.pl -l [length] -c
rand.pl is a Perl script that generates a random string
and writes it to a file or places it on the Windows clipboard
to be pasted later. If no command line options are specified,
a 30,000 character string is placed in the clipboard.
Command line options:
-h Get help using randgen.pl (this file)
-l [length] Set the length of the generated string
(default is 30000)
-c Place the generated string in the Windows clipboard
-f Write the generated string to a file (you'll be
prompted for a path -- default is
c:/docume~1/\%HOME%/desktop/random.txt (your home
directory))
*********************************************************************
END
return;
}
As you can see, if the -f option is passed, I am prompting for save
path in the file_out subroutine. I'd like to do away with that and only
have to run one command and not have to interact with the program
anymore.
So, if I have told perl that f will have a parameter (by adding the
colon in the getopts function), is it true that the -f option gets
completely ignored if there is no parameter passed with it from the
commandline?
How can I check to see if the -f option was passed without a parameter
then?
Thanks for your help.