my first perl, suggestions please.
Hi,
Yesterday I wrote my very first piece of perl code. Now that the initial wave of "I'm so proud of me" has passed I'm curious if I could have done this better. It does what it's supposed to do but I'd like to know if there's anything I could/should improve. ------ #!/usr/bin/perl # postfix log analyzer to count number of messages blocked by dnsbls set in main.cf # version 0.1 # 8 april 2004 # jeroen wijnands # # # postfix log file $file = '/var/log/maillog'; # dnsbls used $dnsbl1 = 'bl.spamcop.net'; $dnsbl2 = 'tw.countries.nerd.dk'; $dnsbl3 = 'cn.countries.nerd.dk'; $dnsbl4 = 'br.countries.nerd.dk'; $dnsbl5 = 'kr.countries.nerd.dk'; $dnsbl6 = 'sbl.spamhaus.org'; $dnsbl7 = 'relays.ordb.org'; $virii = 'virus'; # opening the logfile open (ding, "$file") || die "couldn't open spamcopfile!"; @lines = <ding>; # start counting $count1 = grep { /$dnsbl1/ } @lines; $count2 = grep { /$dnsbl2/ } @lines; $count3 = grep { /$dnsbl3/ } @lines; $count4 = grep { /$dnsbl4/ } @lines; $count5 = grep { /$dnsbl5/ } @lines; $count6 = grep { /$dnsbl6/ } @lines; $count7 = grep { /$dnsbl7/ } @lines; $virus = grep { /$virii/ } @lines; close (ding); $total1 = $count1 + $count2 + $count3 + $count4 + $count5 + $count6 + $count7; $total2 = $total1 + $virus; # just closed the logfile now we start printing # print "aantal berichten door welke dnsbl \n"; print "number of messages blocked by $dnsbl1 : $count1 \n"; print "number of messages blocked by $dnsbl2 : $count2 \n"; print "number of messages blocked by $dnsbl3 : $count3 \n"; print "number of messages blocked by $dnsbl4 : $count4 \n"; print "number of messages blocked by $dnsbl5 : $count5 \n"; print "number of messages blocked by $dnsbl6 : $count6 \n"; print "number of messages blocked by $dnsbl7 : $count7 \n"; print "total number of messages blocked by dnsbl: $total1 \n"; print "\n"; print "number of virii stopped: $virus \n"; print "\n"; print "total of blocked messages: $total2 \n"; # and that's it ---- What I'd still like to add is the total number of messages that have actually reached inboxes on my server but I'm not quite sure how to get that from the log file. |
You could of made it shorter by using an array and loop bits of your code. So if you ever add to the list, it becomes a simple matter or adding one line, and increasing one number.
|
ok, sounds interesting. Any dummy level info on that? This whole programming thing is rather new to me.
|
Its been a while since I've touched perl, but for starters
Code:
$numItems = 7; Code:
for ($i = 1; $i < $numItems; $i++) |
leonscape, thanks a lot for your trouble, I'm going to copy this and try to understand. Really apprieciate it!
|
Code:
@dnsbl = qw(bl.spamcop.net tw.countries.nerd.dk cn.countries.nerd.dk br.countries.nerd.dk); |
and so I live and learn. I must say, perl seems to be a beginner friendl ylanguage.
|
All times are GMT -5. The time now is 03:17 AM. |