Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Constructive Criticism for My First Perl Program

by wog (Curate)
on Sep 13, 2001 at 02:28 UTC ( [id://112045]=note: print w/replies, xml ) Need Help??


in reply to Constructive Criticism for My First Perl Program

=-> perl -c fwanalysis elseif should be elsif at fwanalysis line 197. syntax error at fwanalysis line 197, near ") {" syntax error at fwanalysis line 209, near "}" fwanalysis had compilation errors.

You should fix that problem. And the other compliation error that will appear after you fix that one. Please, test your code before showing it to us.

That said, don't make external calls to date like that, try Date::Format, or strftime from the POSIX module (use POSIX qw(strftime); my $today = strftime('%d%b%y',localtime);). Doing that should make your code more portable.

Also, Mail::Send is probably a better thing to use then sendmail directly (since some systems don't have sendmail, of course.) That may also make your code more portable.

I would also strongly reccommend you indent your subroutines consistently. (Personally I prefer that all subroutines be indented, but I can see a good reason to not do that, in some cases.)

update: Looking at your code with more detail I see this:

my $ip1 = $ips[0][0]; my $times = $ips[0][1]; whois($ip1); evidence($ip1,$times); # Second ip my $ip2 = $ips[1][0]; my $times2 = $ips[1][1]; whois($ip2); evidence($ip2,$times2); # Third ip my $ip3 = $ips[2][0]; my $times3 = $ips[2][1]; whois($ip3); evidence($ip3,$times3); # ...

This is MUCH better written as a loop. For example:

foreach my $i (0..14) { my($ip,$times) = @{$ip[$i]}; whois($ip); evidence($ip,$times); }

And that's still terribly written, too. You should try to make your subroutines be independent of each other -- avoid global variables.

update2: (... pass things as arguments to subroutines instead, or use OO if that is more appropriate.) Another potential problem I see is this:

if ($_ =~ $ip){

The problem with this is that regex metacharacters will be interperated in the IP address, so the .s in the IP address will match any character. For example, if the IP address were 1.2.4.8 then lines of the logfile for the IP address 162.4.83.1 would match that. You might be better of using something like $_ =~ /\Q$ip\E/, or index($_, $ip) != -1.

On this issue of your regex for extracting the domain, etc from the email, note that there are many valid emails that your regex won't match (search for a node on e-mail validation to see why). Consider using Mail::Address instead of a regex.

Replies are listed 'Best First'.
Re: Re: Constructive Criticism for My First Perl Program
by dru145 (Friar) on Sep 13, 2001 at 19:15 UTC
    wog,

    Thanks for all your useful suggestions. The elseif statement was a goof on my part. I originally tested the code without this statement, but added it afterwards to catch emails with no email addresses and didn't test it since. Of course it failed along with a few other things that I found wrong with my code.

    I was thinking about adding a foreach loop also, I've just been spending so much time trying to get the whois working. I think I will use Mail::Send instead of sendmail. The reason I use sendmail is because the perldoc FAQ recommends it: perldoc -q "send mail" but I figure this was written before Mail::Send because they also recommend Mail::Mailer.

    note that there are many valid emails that your regex won't match (search for a node on e-mail validation to see why).

    I already inquired about the email problem Matching any Email Address. I know this regex would not match all email addresses, but it has matched everyone for me so far.

    Again, thanks for the help and I plan on implementing most if not all of your recommendations.

    -Dru

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://112045]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2024-04-23 10:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found