Gossamer Forum
Home : General : Perl Programming :

Logging emails to text files. Help!

Quote Reply
Logging emails to text files. Help!
In one of my forms i have two email fields... One of a Referrer and other of the Referrer's Friend. Now i want to log both of these emails in a text file and also avoid any duplicate entries. Presently i am using the following sub routine to log the Referres's Email address and the same thing repeated with the names of the variables changed to log the Referrer's Friend's Email.

sub Log_Ref_Email {

open(SUB, "subscribelist.txt") || die "Cannot open file $!";
@emails=<SUB>;
close(SUB);

@email = grep{ /$in{'ref_email'}/i } @emails;

if (@email) {

} else {

open(SUB, ">>subscribelist.txt") || die "Cannot open file $!";
print SUB "$in{'ref_name'}\t$in{'ref_email'}\n";
close(SUB);

}
}

But the problem is, i want to avoid writing the same sub routine more than one time and still complete the job. The reason why i want to avoid writing the sub routine more than one time is that the next script i have to code contains 5 user input emails and i am sure there is much simpler and shorter way to do this.

Can anybody help me out with this???

Thanx.
Quote Reply
Re: [Malik] Logging emails to text files. Help! In reply to
You can pass email address to the subroutine, and then replace $in{'ref_email'} with $_[0]. BTW, using grep to check for duplicate entries is not the recommended method (at least for larger files) - check out perldoc.com

----
Cheers,

Dan
Founder and CEO

LionsGate Creative
GoodPassRobot
Magelln
Quote Reply
Re: [dan] Logging emails to text files. Help! In reply to
Thanx Dan, but i didnt get u. how should i pass the email address to the sub routine and how should i actually do that? i'll be thankful to u if u can elaborate ur explanation or gimme an example? BTW i figured out that the 'grep' command is not recommended method coz its a slow process. am i right?
Quote Reply
Re: [Malik] Logging emails to text files. Help! In reply to
Pass email address to function like:

&Log_Ref_Email($in{'ref_email'})

&Log_Ref_Email($in{'friend_email'})

... and replace $in{'ref_email'} in the function with $_[0].

And it's because the grep function slurps the entire file into an array. Better to go through file, one line at a time.

----
Cheers,

Dan
Founder and CEO

LionsGate Creative
GoodPassRobot
Magelln
Quote Reply
Re: [Malik] Logging emails to text files. Help! In reply to
It's not really grep that's the problem, it's how you are looping.

You are slurping the whole file into an array which consumes memory. You should use a while loop.
Quote Reply
Re: [Paul] Logging emails to text files. Help! In reply to
Yep, that's true. The grep function is very powerful, when used correctly - for example, as Paul says, use as you loop through file so you don't slurp entire file into an array. But there are other ways too (which vary in benchmark times). But the main thing is to go through file, one line at a time to test if email address exists. I was only addressing the slurping issue, and did not intend to malign grep.

----
Cheers,

Dan
Founder and CEO

LionsGate Creative
GoodPassRobot
Magelln
Quote Reply
Re: [dan] Logging emails to text files. Help! In reply to
You made me do that Dan! Its workin' great! But as i just dont want to simply copy things without actually understanding how they work, i w'd like to ask you what $_[O] this actually does? Thanx a lot for the pain u r takin' to expalin me all that stuff.
Quote Reply
Re: [Malik] Logging emails to text files. Help! In reply to
@_ is the parameter array. Parameters passed to the function are stored in the @_ array so that the function can retrieve them. For example, $_[0] holds the first element of the @_ array.

----
Cheers,

Dan
Founder and CEO

LionsGate Creative
GoodPassRobot
Magelln
Quote Reply
Re: [dan] Logging emails to text files. Help! In reply to
Thanx for the explanation Dan. It was nice! I am trying to work with the 'while' loop as suggested by Paul. Thanx to you too Paul.Smile
Quote Reply
Re: [dan] Logging emails to text files. Help! In reply to
Hey, i just aint gettin it.Frown how do i check for duplicate entries using a while loop and only print it to the file if the entry doesn't already exists?
Quote Reply
Re: [Malik] Logging emails to text files. Help! In reply to
Phew! At last i think i got it. well the following is my code

sub Log_Ref_Email {
open(SUB, "subscribelist.txt") || die "Cannot open file $!";
while (<SUB>) {
$email = grep{/$_[1]/} <SUB>;
close(SUB);
if ($email) {
} else {
open(SUB, ">>subscribelist.txt") || die "Cannot open file $!";
print SUB "$_[0]\t$_[1]\n";
close(SUB);
}
}
}

where $_[0] refers to the 'Name' and $_[1] to the 'Email addresess' parameters passed to the functions as follows

&Log_Ref_Email($in{'ref_name'},$in{'ref_email'});

&Log_Ref_Email("",$in{'friends_email'});

so now, is this code completely correct and speedy enough yet consuming less memory? BTW will i have any problems if i do not use 'flock'?

i'll be thankful if anybody c'd answer my questions...