LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-09-2004, 04:43 AM   #1
wijnands
Member
 
Registered: Mar 2004
Posts: 132

Rep: Reputation: 15
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.
 
Old 04-09-2004, 05:08 AM   #2
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
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.
 
Old 04-09-2004, 05:10 AM   #3
wijnands
Member
 
Registered: Mar 2004
Posts: 132

Original Poster
Rep: Reputation: 15
ok, sounds interesting. Any dummy level info on that? This whole programming thing is rather new to me.
 
Old 04-09-2004, 05:21 AM   #4
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
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.
 
Old 04-09-2004, 05:28 AM   #5
wijnands
Member
 
Registered: Mar 2004
Posts: 132

Original Poster
Rep: Reputation: 15
leonscape, thanks a lot for your trouble, I'm going to copy this and try to understand. Really apprieciate it!
 
Old 04-09-2004, 11:45 AM   #6
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
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.

Last edited by aluser; 04-09-2004 at 11:50 AM.
 
Old 04-09-2004, 03:10 PM   #7
wijnands
Member
 
Registered: Mar 2004
Posts: 132

Original Poster
Rep: Reputation: 15
and so I live and learn. I must say, perl seems to be a beginner friendl ylanguage.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with perl module for w3c validator to work on my local Apache+PHP+perl instal tbamt Linux - Software 0 12-16-2004 06:37 PM
cgi perl : I cant get perl to append my html file... the_y_man Programming 3 03-22-2004 06:07 AM
Perl cgi suggestions? megaspaz Programming 7 11-10-2003 04:57 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 09:09 PM
chrooting apache v2 (php, ssl, perl support) ; perl configuration markus1982 Linux - Security 3 01-26-2003 07:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration