Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Chomp

Reply
Thread Tools

Chomp

 
 
zoomcart.com
Guest
Posts: n/a
 
      02-20-2009
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
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      02-20-2009
"zoomcart.com" <> wrote in news:6e98e552-1d5e-
4b27-8c10-:

> 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 = <USERS> ) {

> 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 <>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
Reply With Quote
 
 
 
 
Tim Greer
Guest
Posts: n/a
 
      02-20-2009
zoomcart.com wrote:

> 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?
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      02-20-2009
zoomcart.com <> wrote:

> @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;


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      02-21-2009
On Fri, 20 Feb 2009 10:43:53 -0800 (PST), "zoomcart.com" <> wrote:

[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

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"chomp,chop" usage i.e. chop immediately after chomp martin Perl Misc 3 04-15-2006 08:09 PM
Question on Chomp Matt Taylor Perl 2 09-29-2003 11:54 AM
How you chomp in python Fernando Armenta Python 3 09-21-2003 10:09 AM
Re: How you chomp in python Jeremy Dillworth Python 0 09-20-2003 02:36 AM
Chomp not working properly with Sendmail Aaron Powell Perl 0 07-24-2003 02:24 PM



Advertisments