Here are a few comments on slimming down your code while still keeping it readable (or even improving the readability) Of course, this is all My Not So Humble Opinion, so take with salt. Feel free to see this as a vast exercise in Hubris on my part.
#!/usr/bin/perl
require LWP::UserAgent;
require HTTP::Request;
require HTTP::Response;
use HTTP::Request::Common;
First, I'd recommend using perl with the -w (warn) option, and "use strict;" These can save you hours of debugging, and encourage good programming habits. At first it may seem a pain, but with a little practice they add no noticed effort, and you tend to do things a "Right Way" by default.
I'd also "use" all those modules rather than "require"ing them. This imports as the module author intended, and if I disagree, I can override the authors defaults. See
use for details.
foreach (@ARGV)
{
if ( $_ eq $ARGV[0] )
{
$inputfile = $_;
}
elsif ( $_ eq $ARGV[1] )
{
$outdir = $ARGV[1];
}
else
{
die "Usage: $0 inputfile outdir\n";
}
}
This is an unusual way of going about it. You copy the first two arguments, and die if there are more. I prefer the more succint:
die "Usage: $0 inputfile outdir\n" unless scalar @ARGV == 2;
#I prefer "scalar @LIST", some prefer $#LIST,
#but remember the difference
my ($inputfile, $outdir) = @ARGV;
This has the advantage of working as intended (well, dieing as intended) if only one argument is given.
Just one more:
if ($filenum =~ /\d\d\d\d/) {$filenum = $filenum; }
elsif ($filenum =~ /\d\d\d/) {$filenum = "0$filenum"; }
elsif ($filenum =~ /\d\d/) {$filenum = "00$filenum"; }
else {$filenum = "000$filenum"; }
How about:
$filenum = sprintf("%04d", $filenum);
-
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.