LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   my first perl, suggestions please. (https://www.linuxquestions.org/questions/programming-9/my-first-perl-suggestions-please-168040/)

wijnands 04-09-2004 03:43 AM

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.

leonscape 04-09-2004 04:08 AM

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.

wijnands 04-09-2004 04:10 AM

ok, sounds interesting. Any dummy level info on that? This whole programming thing is rather new to me.

leonscape 04-09-2004 04:21 AM

Its been a while since I've touched perl, but for starters

Code:

$numItems = 7;
&dnsbl = { 'bl.spamcop.net', 'tw.countries.nerd.dk',
                  'cn.countries.nerd.dk', 'br.countries.nerd.dk',
                  'kr.countries.nerd.dk', 'sbl.spamhaus.org',
                  'relays.ordb.org' };

To setup the array, then to loop through it

Code:

for ($i = 1; $i < $numItems; $i++)
{
  $count[i] = grep { /$dnsbl[i]/ } @lines;
}

You may need to change these as I'm a bit rusty but you get the general idea. There may also be better ways of doing it, with lists etc.

wijnands 04-09-2004 04:28 AM

leonscape, thanks a lot for your trouble, I'm going to copy this and try to understand. Really apprieciate it!

aluser 04-09-2004 10:45 AM

Code:

@dnsbl = qw(bl.spamcop.net tw.countries.nerd.dk cn.countries.nerd.dk br.countries.nerd.dk);
my $i = 0;
my $total = 0;
$total += grep { /$dnsbl[$i++]/ } @lines for @dnsbl;
print "total thingies: $total\n";

Would probably work. That counts as either succinct or obfuscated, depending on your preferences.

wijnands 04-09-2004 02:10 PM

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.