Ittiam Systems Interview Question
Software TraineesTeam: dsfgfds
Country: India
Interview Type: Written Test
The return value of any block, including those used in subroutines, is taken as the value of the last evaluated expression. For exampl,e the return value here is the result of the calculation.:
sub myfunc
{
$_[0]+$_[1];
}
You can also explicitly return a value using the return keyword:
sub myfunc
{
if (@_)
{
return $_[0]+$_[1];
}
else
{
return 0;
}
}
When called, return immediately terminates the current subroutine and returns the value to the caller. If you don't specify a value then the return value is undef.
Simplest way is to [a-zA-Z0-9][a-zA-Z0-9\.\-_]*@[a-zA-Z0-9]\.(com|ca|com\.uk|...)
Validating email addresses isn't easily verifiable using regex. Even if it's considered invalid according to the RFC standard, the rules on some SMTP/IMAP servers vary so much, it's much easier just to send a email verification link to that email. (Which is what most software does. OAuth mitigates this terribly inconsistent protocol somewhat.)
Actually, while I agree the parsing of email addresses is complex, most of the complexity is in the local part of the name. Also, using \p{IsWord} will match UNICODE characters which are valid in email addresses as well. So, \p{IsWord} is probably prefered over \w or [a-zA-Z] forms. When addressing this kind of problem, I would recommand:
my %extmap = (
com => '',
edu => '',
...,
)
if(! emailValidate("someone@somewhere.somplace") ) {
print "email is not valid\n";
} else {
print "email is vaild\n";
}
sub emailValidate {
my ($email) = @_;
my ($rpt,$hsn) = (split(/\@/,$email);
# email is not valid if there is not a localname and hostname seperated by @
# note, I don't test for multiple @
return (0) if( ! defined $rpt || ! defined $hsn );
# rpt@[1.1.1.1] is valid for email to an IP. However, the regex I provide does not
# really make sure the IP is valid, only that it follows the pattern.
if( $hsn !~ m/^\[(\d{1,3}\.){3}\d{1,3}\]$/ ) {
return(0);
} else {
my $ext;
foreach(split(/\./,$hsn) {
# not perfect, but allows for h@(comment)blah.com or h@blah.com
# also invalidates h@blah..com
if( $_ !~ m/^\w[\w|\d]*$/g || $_ !~ m/^\([\w|\d]+\)\w[\w|\d]*$/ ) {
return(0);
}
$ext = $1; # save last result
}
if( ! exist $extmap{$ext} ) {
return(0);
}
}
# \W is imprecise, but () # \ {} and a host of other character marks are valid.
# A fairly detailed understand of either the RFC or the standards set by
# The local MTA would really be necessary. Things like .. are not allowed but . is.
if( $rpt !~ m/^\p{IsWord}[\p{IsWord}|\d\W]*$/ || $rpt !~ m/^\p{IsWord}[\p{IsWord}|\d\W]*(\(\w\))*$/) {
return(0);
}
return(1);
}
re.compile("^[a-zA-Z][\w]*@[\w]+\.[a-zA-z]+$")
- gnahzy August 29, 2012