LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-24-2013, 12:09 PM   #1
vikky
LQ Newbie
 
Registered: Apr 2012
Posts: 26
Blog Entries: 1

Rep: Reputation: Disabled
perl script for checking an email id


[root@server perl.pl]# perl perl16.pl
Backslash found where operator expected at perl16.pl line 6, near "_\"
syntax error at perl16.pl line 6, near "_\"
Execution of perl16.pl aborted due to compilation errors.
[root@server perl.pl]# vi perl16.pl
#!/usr/bin/perl
use warnings;
use strict;
my $pattern=<STDIN>;
chomp($pattern);
if(/$pattern=~/^(\w|\_\|\-|\.)+\@((\w|\-|\_)+\.)+[a_zA_Z]/)
{
print "/$patter is an email id .\n";
else{
print "/$patter is not a email id .\n"
}
let me know why this script is failing

Thanks in advance
 
Old 02-24-2013, 12:42 PM   #2
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,117

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Quote:
Originally Posted by vikky View Post
let me know why this script is failing
Quote:
if(/$pattern=~/^(\w|\_\|\-|\.)+\@((\w|\-|\_)+\.)+[a_zA_Z]/)
Leading slash in front of $pattern (definite syntax error), backslash in front of pipe char (this won't cause a syntax error, but probably won't work the way you expect), underscores rather than dashes in alpha range at the end (again, not a syntax error, but probably won't work as you intend).

Try this:
Code:
if($pattern=~/^(\w|\_|\-|\.)+\@((\w|\-|\_)+\.)+[a-zA-Z]/) {
If you want to get even pickier, you've got a few unnecessary backslashes, but they won't hurt anything if left there. Here it is with those extra backslashes removed:
Code:
if($pattern=~/^(\w|_|-|\.)+\@((\w|-|_)+\.)+[a-zA-Z]/) {
I didn't look to see if your pattern matching will do anything useful in matching email id's, I just looked at it from a PERL/syntax perspective.

Also, PERL's \w will match an _, so you don't need to have a specific for that. So simplifying even more:

Code:
if($pattern=~/^(\w|-|\.)+\@((\w|-)+\.)+[a-zA-Z]/) {
Quote:
print "/$patter is an email id .\n";
No doubt you mean $pattern, not $patter. However, this error should be picked up at runtime by you including "use strict". I don't know why you want that slash in front of what you're printing, but that's not an error.

Not wanting to burst your bubble, but things are simpler than you are making them. Check out:

http://search.cpan.org/~rjbs/Email-V...Email/Valid.pm

Last edited by haertig; 02-24-2013 at 01:02 PM.
 
Old 02-24-2013, 02:30 PM   #3
vikky
LQ Newbie
 
Registered: Apr 2012
Posts: 26
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
yeah that was an typo error..that i have give \before $pattern

but, can you tell me :

if($pattern=~/^(\w|-|\.)+\@((\w|-)+\.)+[a-zA-Z]/)

why you have given \before . when you are using or operator in between...

please help as i'm newer to this....and you haven't use \before -

Thank You
 
Old 02-24-2013, 05:45 PM   #4
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,117

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Because . (dot) means "match any character". You don't want that in your example here. You want dot to match a dot and only a dot. So you have the backslash it, to escape its normal meaning of "match any character".

Without that backslash, the expression (\w|-|.) would mean "match a word character, or match a dash, or match any character" That final "or match any character" would make the first two specifications, \w and -, useless and redundant. Plus, it wouldn't do what you want, which in this case is "match a dot and only a dot".

You might want to say, "but what about a - (dash), doesn't that have special meaning too, so why doesn't it have to be escaped?"

The reason is, a dash only has special meaning in one context, inside a range, as in "[a-z]" or "[0-9]". If it is not in this context, it has no special meaning, therefore it doesn't need to be escaped. But you could escape it if you wanted. That wouldn't hurt anything. You see that fairly commonly. What it means is that the author did not know if the dash needed to be escaped or not, so they escaped it just to be sure.

[a-z] would match "any lowercase letter". [a\-z] would match "the letter a, or the letter z, or a dash"

To make things even more confusing, a backslash can do different things ... it can either remove special meaning as in "\.", or it can ADD special meaning as in "\w". In this second case, without the backslash, "w" means (obviously) the letter w. But putting a backslash in front of it ADDS special meaning, thus "\w" means "any word character".

Last edited by haertig; 02-24-2013 at 05:48 PM.
 
2 members found this post helpful.
Old 02-25-2013, 11:13 PM   #5
vikky
LQ Newbie
 
Registered: Apr 2012
Posts: 26
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Thank You so much haertig appreciated your help...
 
Old 02-25-2013, 11:20 PM   #6
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
if they helped, please mark this as [SOLVED] you will find it at the top of the thread.
 
  


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
[SOLVED] question concerning postfix, a perl script, and Amazon Simple Email Service sneakyimp Linux - Server 8 11-12-2012 06:20 PM
Cron Email Notification - Perl Script jamesyreid Programming 3 07-21-2009 08:56 AM
Perl email script fails cov Programming 2 03-27-2009 12:17 PM
Changing the mail Header using Perl Email Script athreyavc Programming 1 11-28-2008 08:07 AM
LXer: Perl script to monitor disk space and send an email alert LXer Syndicated Linux News 1 02-23-2007 02:12 PM


All times are GMT -5. The time now is 08:33 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