?
Solved

how to exit out og a function ?

Posted on 2003-03-21
6
Medium Priority
?
188 Views
Last Modified: 2010-03-04
Hi ,
I have a small mailing_list script.
i have re-written it to have some function ( so it'll be a bit more readable )
i loop through a list of emails and run functions on them.
one of the functions validates the email addres :
------------------------------------------------------
sub validate_email_addr{
  if(!Email::Valid->address($email_addr[0])){
    print "$email_addr[0] - invalid\n";
    next;  
  }
}#end sub
-------------------------------------------------------
i get this error when an email is invalid .
Exiting subroutine via next at /var/bvg/bin/submailer.pl line 321 (#1)
   
    (W) You are exiting a subroutine by unconventional means, such as
    a goto, or a loop control statement.

the question is what is the "conventional means" to skip to the next record if
the current is invalid ?
thanks
barazani

0
Comment
Question by:barazani
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 6

Expert Comment

by:holli
ID: 8181664
if want to jump out of a function use the return statement.

sub validate_email_addr{
 if(!Email::Valid->address($email_addr[0])){
   print "$email_addr[0] - invalid\n";
   return;  
 }
}#end sub
0
 
LVL 6

Expert Comment

by:holli
ID: 8181681
by the way, the above is no error but a warning (W). in most cases you can ignore them.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 8183736
if (condition) {
is a conditional statement (not a loop) that if the condition is met will execute the statement(s) within that block and exit.

next;
is used to jump to the next iteration of a loop (e.g. for, while, until, etc) skipping any additional statements within the loop.

If you need to loop through the (condition), then you should to change it to a loop block and pass a variable containing all email addresses to the sub or you can call the sub each time you need to verify a single email address.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 28

Accepted Solution

by:
FishMonger earned 500 total points
ID: 8183859
By the way, putting in,
return;
as Holli shows, may not necessary and in the given code could be removed.


Quoting Perl in a Nutshell on the use of:  return expr

"Returns from a subroutine (or eval) with the value of expr.  (In the absence of an explicit return, the value of the last expression evaluated is returned.)  Use of return outside of a subroutine or eval will result in a fatal error.  The supplied expression will be evaluated in the context of the subroutine invocation.  That is, if the subroutine was called in a scalar context, expr is also evaluated in scalar context.  If the subroutine was invoked in a list context, then expr is also evaluated in a list context, and can return a list value.  A return with no argument returns the undefined value in a scalar context, and a null in list context.  The context of the subroutine call can be determined from within the subroutine by using the (misnamed) wantarray function."
0
 
LVL 48

Expert Comment

by:Tintin
ID: 8184369
I must admit, I can't see why you would want to create a subroutine for a one line action like:

print "$mail_addr[0] - invalid\n" unless (Email::Valid->address($email_addr[0]));
0
 

Author Comment

by:barazani
ID: 8202574
Tintin - thanks , you are right since i'm a sysadmin i think in shell not in perl ...
Fishmonger - thanks for the answer it explains a lot .
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question