[LA.pm] GetOpts::Long weirdness
Ofer Nave
onave at bizrate.com
Tue Oct 26 17:30:58 CDT 2004
Oh boy...
Well, I'll answer your question first. Kind of. I copied your code
verbatim, and it works fine for me.
Add this after the call to GetOptions():
print "help=$help\n";
exit;
Now, if I run the script with no params, help is undefined. If I run it
with --help, help=1. So it looks fine.
However... you really need to use strict. There's pretty much no
rational reason not to, and every reason to do so. I also think
diagnostics is generally overkill, but if its only for development and
you like it, that's cool.
BTW-You're getting some format redifined warnings.
-ofer
Nicholas Bernstein wrote:
>I was wondering if someone could take a look at this and tell me why
>$help is not being defined when the program is run with --help as a
>command line option. The rest of it is working fine, just for some
>reason GetOptions doesn't want to define it (why do the easy parts
>always break?) anyway, since I've been looking at this for too long, and
>I'm sure that all of you know a bunch more than I do, please help
>enlighten me.
>
>Thanks in advance,
>Nick
>
>#--------------------- start -------------------#
>
>#!/usr/bin/perl
>
>use warnings;
>use diagnostics;
>
># CPAN Modules
># -----------------
>
>use Getopt::Long;
>use Digest::MD5;
>
># Definitions
># -----------------
>
>$XML_DIR = "/u/dsi/xml/";
>$host1 = "localhost";
>$host2 = "localhost";
>$FM = "/u/fillmagic/production/bin/fillmagic.sh";
>$wait = "1";
>
>
>GetOptions(
> "help" => \$help,
> "host1|h1=s" => \$host1,
> "host2|h2=s" => \$host2,
> "xml|x=s" => \$XML_DIR,
> "wait|w=i" => \$wait
>
>) or die "GetOptions can't get variable";
>
>sub print_help_msg {
> print "
> This program is used to test a new fillmagic server against a known good fillmagic server.
> It runs a fill against each server, computes a md5 checksum of the response, and compares the
> two.
>
> The minimum options to give this program are host1 and host2. The order does not matter.
> This is an example of the using filltester.pl with the minimum number of arguments:
>
> filltester.pl -h1 foo.foo.com -h2 bar.foo.com
>
> In this case the time waited in between responses will be 1, and the xml directory will be
> '/u/dsi/xml/'
>
> here is the same example with a three second wait, and a different xml directory:
>
> filltester.pl -h1 foo.foo.com -h2 bar.foo.com -w 3 -x /usr/local/fillmagicstuff/xml/
>
> Here is a fill list of the options for this command:
>
> --host1, -h1)
> define host1
>
> --host2, -h2)
> define host2
>
> --xml, -x)
> define the xml directory
>
> --wait, -w)
> define the time to wait in between processing.
>
> --help, -h)
> prints this screen
>";
>exit()
>}
>
>
>sub getfiles {
> opendir( "XMLDIR", "$_[0]");
> my @files = readdir("XMLDIR");
> closedir("XMLDIR");
> return @files;
>}
>
>sub do_fillmagic {
>
> # $sum = do_fillmagic("da.docmagic.com", "some_xml_file.xml");
> # do fillmagic takes a hostname, and an xml file to post
> # and returns an md5 digest of the output. You can
> # then compare this output vs another known good server
> # and verify that the results are the same.
>
> my $host = $_[0];
> my $file = $_[1];
> my $url = "http://" . $host . "/fillmagic/servlet/fillservlet";
> my $pwd = $ENV{'PWD'};
> my $outfile = "$pwd/out." . $host;
> my $reqfile = "$pwd/cachetestrequest.xml ";
>
> # run the fillmagic request
> # --------------------------
>
> my $cmd = $FM . " -r " . $reqfile . $file . " -w " . $url . " -o " . $outfile ;
>
> $ENV{'DSI_HOME'} = "/u/fillmagic/production/";
>
> $toss=" "; # just gets rid of variable was only used once warning
> $toss=`$cmd || echo "failed"` or die("Could not run System Command:\n$cmd");
>
> # read the output
> # --------------------------
> open(MYFILE, "<$outfile") or die("could not open file: $outfile \n It was probably not created by:\n\"$cmd\"\n");
> @lines = <MYFILE> ;
>
> # create md5sum
> # --------------------------
> $ctx = Digest::MD5->new;
> foreach $line (@lines) {
> $ctx->add($line);
> }
> my $digest = $ctx->hexdigest;
>
> # Remove the output and return the results
> # --------------------------
> close("MYFILE");
> unlink($outfile);
> return($digest) ;
>}
>
>sub run_and_compare {
> my $hostA = $_[0];
> my $hostB = $_[1];
> my $file = $_[2];
> my $md51 = do_fillmagic("$hostA", $file) unless ( ! -f $file) ;
> my $md52 = do_fillmagic("$hostB", $file) unless ( ! -f $file);
>
> if ( $md51 ne $md52 ) {
> #print "$file: ... FAILED\n";
>format STDOUT =
>@<<<<<<<<<<<<<< @<<<<<<<<<<<<
>$file, FAILED
>.
> write ;
> } else {
>format STDOUT =
>@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<
>$file, OK
>.
> write;
> }
>}
>
># main
># ---------------------------------------------------------------
>
>if ( defined($help) ) {
> print_help_msg();
>}
>
>@files = getfiles($XML_DIR);
>
>foreach $file ( @files ) {
> if (
> ( $file =~ m/^\./ ) or
> ( $file =~ m/^\.\./ )
> ) {
>
> #Skipping dotfile
>
> } else {
>
> qr/$file/ ;
> $file= $XML_DIR . $file;
>
> if ( -f $file ) {
>
> run_and_compare($host1, $host2, "$file");
> sleep($wait);
>
> } elsif ( -d $file ){
>
> # Skipping Directory $file
>
> }
> }
> }
>
>
># -------------------- end ---------------------#
>
>
>CONFIDENTIALITY NOTICE: This email and any attachments thereto may contain information which is privileged and confidential, and is intended for the sole use of the recipient(s) named above. Any use whatsoever of the information contained herein (including, but not limited to, total or partial reproduction, communication or distribution in any form) by persons other than the designated recipient(s) is strictly prohibited. If you have received this email in error, please notify the sender either by telephone, by email, or by any other written or telephonic means, and delete the material from any computer. Thank you for your cooperation.
>
>
>_______________________________________________
>Losangeles-pm mailing list
>Losangeles-pm at mail.pm.org
>http://mail.pm.org/mailman/listinfo/losangeles-pm
>
>
More information about the Losangeles-pm
mailing list