Chomp

Z

zoomcart.com

Hello and thanks in advance for you help.
I have a simple email program that has a bulk add feature where a user
can add an email list separated by returns. It works fine, except when
the user tries to edit and remove an email, the script is not
recognizing the email. If the email was added individually, it
recognizes it. This may only be caused when the user is using a mac. I
have tried several chomp variations.
This is the bulk add code..
sub add_bulk_email{
my ($newemail, $bademail, $email, $newemail, $checkemail);
$checkemail = param('checkemail');
@emails = split(/\n+/, $checkemail);
foreach $email(@emails){
unless ($email =~ /.*\@.*\..*/) {
$bademail=$email;
}
else{
$emailcnt++;
chomp $email;
#chomp $email if ($email =~ /\n$/);
#chomp $email if ($email =~ /\r$/);
$newemail .= "$email\n";
}
}
if($newemail){
open (USERS, ">>$userpath/lists/$list") || &adminerror("$userpath/
lists/$list Update Account" , __FILE__, __LINE__,);
print USERS $newemail;
close (USERS);
&success("$emailcnt Bulk emails have been added to the list");
}#new email
else{ &adminerror("There were No REAL emails in your list");
}
##########################
This is the code for the individual add...
sub add_new_email
{
my ($new_email, @fields, $checkemail, $line);
$checkemail = param('checkemail');
unless ($checkemail =~ /.*\@.*\..*/) {
&adminerror("Please incude a REAL email address.");
}
if($checkemail eq ""){
&adminerror("Please include an email address");
}
chomp $checkemail;
if(-e "$userpath/lists/$list"){
open (USERS, "$userpath/lists/$list") || &adminerror("$userpath/
lists/$list add new email " , __FILE__, __LINE__,);
flock(USERS, LOCK_EX);
while (<USERS>)
{
$line = $_;
chomp $line if ($line =~ /\n$/);
if ($checkemail eq $line){
$newemail = "";
}
else{
$newemail = $checkemail;
}#else
} # End of while (<USERS>)
close (USERS);
}
else{ &adminerror("$list was not found");}
if($newemail){
open (USERS, ">>$userpath/lists/$list") || &adminerror("$userpath/
lists/$list Update Account" , __FILE__, __LINE__,);
print USERS "$newemail\n";
close (USERS);
&success("$checkemail has been added to $list");
}#new email
else{ &adminerror("$checkemail is already on the list");
}
}#update
#########################
And this is the code for the remove email...
sub adminremove_email
my ($new_data, @fields, $old_data, $removeemail, $line);
$removeemail = param('checkemail');
if($removeemail eq ""){
&adminerror("You must include an email");
}
else{
$new_data = "";
open (USERS, "$userpath/lists/$list") || &adminerror("$userpath/
lists/$list Update" , __FILE__, __LINE__,);
flock(USERS, LOCK_EX);
while (<USERS>)
{
$line = $_;
#chop $line;
chomp $line;
#chomp ($line);
#chomp ($line) if ($line =~ /\n$/);
#chomp $line if ($line =~ /\n$/);
#chomp $line if ($line =~ /\r$/);
if ($removeemail ne $line){
$new_data .= "$line\n";
}
else{
$old_data .= "$line\n";
}#else
} # End of while (<USERS>)
close (USERS);
if($old_data){
open (USERS, ">$userpath/lists/$list") || &adminerror("$userpath/
lists/$list Update Account" , __FILE__, __LINE__,);
print USERS "$new_data";
close (USERS);
&success("$removeemail has been removed");
}
else{
&adminerror("$removeemail was not found");
}
}#we have an email
}#adminremove_email
 
A

A. Sinan Unur

Hello and thanks in advance for you help.
I have a simple email program that has a bulk add feature where a user
can add an email list separated by returns. It works fine, except when
the user tries to edit and remove an email, the script is not
recognizing the email. If the email was added individually, it
recognizes it. This may only be caused when the user is using a mac.

Line endings are different on the Mac CRLF for DOSish systems, LF for
*nixes and CR for the old Mac OS.

The code you have provided below is simply an undreadable mess. Don't
code like this but more importantly, don't post like this. Please take a
look at the Posting Guidelines to learn how to help others help you.
while (<USERS>)
{
$line = $_;

while ( my $line = said:
chomp $line if ($line =~ /\n$/);

A line, by definition, ends with a newline.

Assuming whitespace at the end of an email address cannot be significant
(the assumption makes sense to me but I don't know the RFC), how about:

$line =~ s/\s+$//;

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
T

Tim Greer

zoomcart.com said:
Hello and thanks in advance for you help.
I have a simple email program that has a bulk add feature where a user
can add an email list separated by returns. It works fine, except when
the user tries to edit and remove an email, the script is not
recognizing the email. If the email was added individually, it
recognizes it. This may only be caused when the user is using a mac. I
have tried several chomp variations.
This is the bulk add code..
sub add_bulk_email{
my ($newemail, $bademail, $email, $newemail, $checkemail);
$checkemail = param('checkemail');
@emails = split(/\n+/, $checkemail);
foreach $email(@emails){
unless ($email =~ /.*\@.*\..*/) {

As a couple of people have already covered a lot of problems with the
code in other replies, I'll be short. The above check, just so you
know, will match completely invalid addresses, including seeing "@." as
a valid email address.
$emailcnt++;
chomp $email;
#chomp $email if ($email =~ /\n$/);
#chomp $email if ($email =~ /\r$/);

Line feeds/new lines are not the same across all systems. Why not just
strip out any whitespace/new lines altogether from the variable?
 
T

Tad J McClellan

zoomcart.com said:
@emails = split(/\n+/, $checkemail);


That line of code looks familiar...

.... it appears I posted it verbatim in a followup to you in April 2008.

You're still working on the same program?

if($checkemail eq ""){
&adminerror("Please include an email address");
}


I would prefer writing that as:

adminerror("Please include an email address") unless length $checkemail;
 
S

sln

[snip]
I started off with good intentions to read this code,
but stopped after gazing down at it.
Maybe I will read it if its cleaned up with big indentation,
and uncommented with each line of code as a comment.

-sln
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top