For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2007 > problem with readline









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author problem with readline
Mathew Snyder

2007-06-23, 3:59 am

I have a script that queries a database, grabs a bunch of email addresses from
it and generates a procmail ruleset for each of them. It also opens a file
which contains additional email address and reads them into an array:

open AUTHFILE, "</home/customercare/authorized_users.txt" or die "Can't open
file: $!";
foreach my $address (readline AUTHFILE){
chomp($address);
next if $address =~ m/^#/gmx;
push @email_list, $address;
}

This seems to have worked at one point but appears to have been changed and
doesn't work anymore. The script runs fine with the exception of the part that
opens the file and reads the addresses from it. To be clear, it seems to open
the file. At least, I'm not getting any errors from the "or die" part.

However, when I look for the addresses read in from the file, nothing shows up.
Can someone help me shed some light on this? The full script is below.

Thanks
Mathew


#!/usr/bin/perl

use strict;
use warnings;
use DBI;

my $message = "You are receiving this message in response to an email you"
." sent to customercare\@company.com. The email address you sent"
." from is not currently in our records. To have the action you"
." need performed, please call customer care to authenticate.";

my $dbh = DBI->connect("dbi:Pg:dbname=xx;host=10.0.2.30", "xxxxx", "xxxxxx");
my @email_list;
my $sth;
my @fields = qw/email email2 email3/;

foreach my $field (@fields){
$sth = $dbh->prepare("
SELECT $field
FROM contacts");
$sth->execute;
while ((my $line) = $sth->fetchrow()) {
if ($line){
if ($line !~ /^\s+$/g){
push @email_list, $line;
}
}
}
$sth->finish;
}


#Read in people from the authorized_users.txt file in the customercare directory
#These are email addresses that are not in OSS but are allowed to create tickets

open AUTHFILE, "</home/customercare/authorized_users.txt";
while (<AUTHFILE> ) {
chomp;
next if $_ =~ m/^#/gmx;
push @email_list, $_;
}

print("
#This File is generated by proclmailgen.pl and should not be edited directly.
#It will be overwritten the next time the cron job runs.

VERBOSE=off
PMDIR=\$HOME/.procmail
DEFAULT=/var/spool/mail/customercare
LOGFILE=\$PMDIR/log
LOCKFILE # removes any preexisting lockfile
LOG=\`lockfile \$DEFAULT\$LOCKEXT\`
TRAP=\"rm -f \$DEFAULT\$LOCKEXT\"
");

printf("
:0 E
* ^From.*(customercare\@company.com)
badmail
");

printf("
:0 E
* ^Return-Path.*(customercare\@company.com)
badmail
");

printf("
:0 E
* ^X-Loop.*(customercare\@company.com)
badmail
");

for my $address (@email_list){
$address =~ s/ //g;
printf("
:0 E
* ^From.*(%s)
| /usr/local/rt-3.6.0/bin/rt-mailgate --queue customercare --action correspond
--url https://rt.company.com/
" , ($address));
}

printf("
:0 E
* ^From(.*)
| (formail -r -A\"X-Loop: customercare\@company.com\" ; echo \"%s\") |
/usr/sbin/sendmail -f nobounce\@company.com -t
" , ($message));

--
Keep up with me and what I'm up to: http://theillien.blogspot.com
Tom Phoenix

2007-06-23, 6:59 pm

On 6/23/07, Mathew Snyder <theillien@yahoo.com> wrote:

> foreach my $address (readline AUTHFILE){
> chomp($address);
> next if $address =~ m/^#/gmx;


The author of that code probably doesn't know what /g, /m, and /x do
for a pattern match. When you know how to use them, they're powerful
tools. In this case, they break your code, and I'd get rid of all
three.

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training
Mathew

2007-06-23, 6:59 pm

Tom Phoenix wrote:
> On 6/23/07, Mathew Snyder <theillien@yahoo.com> wrote:
>
>
> The author of that code probably doesn't know what /g, /m, and /x do
> for a pattern match. When you know how to use them, they're powerful
> tools. In this case, they break your code, and I'd get rid of all
> three.
>
> Hope this helps!
>
> --Tom Phoenix
> Stonehenge Perl Training
>


I do think he based his use of it on PBP. I can't see any other reason
for him to use them.

Mathew
Keep up with my goings on at http://theillien.blogspot.com

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com