Welcome to Dream.In.Code
Getting Help is Easy!

Join 86,269 Programmers. There are 1,873 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

Passing return value between sub routines in perl

 
Reply to this topicStart new topic

Passing return value between sub routines in perl, Passing return value between sub routines in perl

siniks
post 30 Apr, 2008 - 01:03 PM
Post #1


New D.I.C Head

*
Joined: 30 Apr, 2008
Posts: 4



/* Here's a section of Perl program with two sub routines, what we are trying to accomplish is when the perl programs calls the "ERROR_ROUTINE" section it assign a value say "3" to a variable and pass it along to another sub routine "End_Function" and in the second Sub routine it been compared and exit with the status...now the issue is we are having trouble to pass the value "ORDER_RETURN_CODE" as a return values from one sub routine to another...am i missing some thing?? please help */

sub Error_Routine {
$ORDER_RETURN_CODE = 3;
system(" perl $script_path/common/error_routine.pl $script_path $script_name $message $attachment " );
return $ORDER_RETURN_CODE;
print ("RUN_ORDER_RETURN_CODE_Error: $ORDER_RETURN_CODE\n");
print ("\n");
&End_Function($ORDER_RETURN_CODE);
}

sub End_Function()
{
#$ORDER_RETURN_CODE = $ARGV[0];
my $ERR = shift;
# if (-e "$script_path/log/${scenario}_in_process") {
# system ("rm $script_path/log/${scenario}_in_process");
# }
print ("Exiting $script_name\n");
print ( "\n");
#if ( $ORDER_RETURN_CODE != 0 ) {
if ( $ERR == 3 ) {
exit 3;
} else {
print ( "\n");
exit 0;
}
}
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


fahlyn
post 30 Apr, 2008 - 05:46 PM
Post #2


New D.I.C Head

*
Joined: 3 Nov, 2007
Posts: 41

Just at a glance it looks like you'll never make it to where you're calling your End_Function() method. The 3rd statement in your Error_Routine method is a "return..."....which will cause it to exit that method...


As a best practice you should only ever have 1 "return" statement in a method and it should always be at the end of the method.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

KevinADC
post 30 Apr, 2008 - 11:49 PM
Post #3


D.I.C Head

Group Icon
Joined: 23 Jan, 2007
Posts: 168

QUOTE(fahlyn @ 30 Apr, 2008 - 05:46 PM) *

Just at a glance it looks like you'll never make it to where you're calling your End_Function() method. The 3rd statement in your Error_Routine method is a "return..."....which will cause it to exit that method...


As a best practice you should only ever have 1 "return" statement in a method and it should always be at the end of the method.



You are correct in your first comment but not in your last. There is no best practice in perl about putting only one return function in a subroutine and only at the end. The stated purpose of the return function is to "get out of a function (or subroutine) early". It is perfectly acceptable to put multiple returns in a function, even at the beginning, for example:

CODE
sub foo {
   my @data = @_ or return(0);#failure so return immeadiately
   for (@data) {
      $_++;
      return(0) if $_ > 100;#failure so return immeadiately
   }
   return @data;#success
}



You often see return(0) at the beginning of a method/function if no data was sent to the function. That of course avoids unecessary processing and returns a false value to the caller to let it know it failed. You can also retun early for a true condition if the situation calls for it. That is common when processing a list and you only need to find the first true condition.

Perl actually retuens the last expression evaluated if no return function is used so strictly speaking its not even necessary to put a return at the end of a function if the last expression evaluated is going to be returned, which is typically the case.

User is offlineProfile CardPM
Go to the top of the page
+Quote Post

KevinADC
post 1 May, 2008 - 12:06 AM
Post #4


D.I.C Head

Group Icon
Joined: 23 Jan, 2007
Posts: 168

QUOTE(siniks @ 30 Apr, 2008 - 01:03 PM) *

/* Here's a section of Perl program with two sub routines, what we are trying to accomplish is when the perl programs calls the "ERROR_ROUTINE" section it assign a value say "3" to a variable and pass it along to another sub routine "End_Function" and in the second Sub routine it been compared and exit with the status...now the issue is we are having trouble to pass the value "ORDER_RETURN_CODE" as a return values from one sub routine to another...am i missing some thing?? please help */

sub Error_Routine {
$ORDER_RETURN_CODE = 3;
system(" perl $script_path/common/error_routine.pl $script_path $script_name $message $attachment " );
return $ORDER_RETURN_CODE;
print ("RUN_ORDER_RETURN_CODE_Error: $ORDER_RETURN_CODE\n");
print ("\n");
&End_Function($ORDER_RETURN_CODE);
}

sub End_Function()
{
#$ORDER_RETURN_CODE = $ARGV[0];
my $ERR = shift;
# if (-e "$script_path/log/${scenario}_in_process") {
# system ("rm $script_path/log/${scenario}_in_process");
# }
print ("Exiting $script_name\n");
print ( "\n");
#if ( $ORDER_RETURN_CODE != 0 ) {
if ( $ERR == 3 ) {
exit 3;
} else {
print ( "\n");
exit 0;
}
}


As fahlyn said, as soon as Error_Routine evaluates this line:

CODE
return $ORDER_RETURN_CODE;


it leaves the subroutine and goes back to where ever it was called from. It will not do any good to put it at the end since End_Function exits. On a side note you should avoid parenthesis around print commands:

CODE
print ("Exiting $script_name\n");


print is not a function, although it is listed as such in the perl documentation, parentheses are used to let perl know a bareword followed by parentheses is a function, for example:

CODE
foo();


from the print man page:

QUOTE
Also be careful not to follow the print keyword with a left parenthesis unless you want the corresponding right parenthesis to terminate the arguments to the print--interpose a + or put parentheses around all the arguments.


regular old print commands should just use quotes or quote operators:

CODE
print  "Exiting $script_name\n";
print qq{Exiting $script_name\n};


See the print man page for more details.



User is offlineProfile CardPM
Go to the top of the page
+Quote Post

siniks
post 1 May, 2008 - 01:14 PM
Post #5


New D.I.C Head

*
Joined: 30 Apr, 2008
Posts: 4

QUOTE(KevinADC @ 1 May, 2008 - 12:06 AM) *

QUOTE(siniks @ 30 Apr, 2008 - 01:03 PM) *

/* Here's a section of Perl program with two sub routines, what we are trying to accomplish is when the perl programs calls the "ERROR_ROUTINE" section it assign a value say "3" to a variable and pass it along to another sub routine "End_Function" and in the second Sub routine it been compared and exit with the status...now the issue is we are having trouble to pass the value "ORDER_RETURN_CODE" as a return values from one sub routine to another...am i missing some thing?? please help */

sub Error_Routine {
$ORDER_RETURN_CODE = 3;
system(" perl $script_path/common/error_routine.pl $script_path $script_name $message $attachment " );
return $ORDER_RETURN_CODE;
print ("RUN_ORDER_RETURN_CODE_Error: $ORDER_RETURN_CODE\n");
print ("\n");
&End_Function($ORDER_RETURN_CODE);
}

sub End_Function()
{
#$ORDER_RETURN_CODE = $ARGV[0];
my $ERR = shift;
# if (-e "$script_path/log/${scenario}_in_process") {
# system ("rm $script_path/log/${scenario}_in_process");
# }
print ("Exiting $script_name\n");
print ( "\n");
#if ( $ORDER_RETURN_CODE != 0 ) {
if ( $ERR == 3 ) {
exit 3;
} else {
print ( "\n");
exit 0;
}
}


As fahlyn said, as soon as Error_Routine evaluates this line:

CODE
return $ORDER_RETURN_CODE;


it leaves the subroutine and goes back to where ever it was called from. It will not do any good to put it at the end since End_Function exits. On a side note you should avoid parenthesis around print commands:

CODE
print ("Exiting $script_name\n");


print is not a function, although it is listed as such in the perl documentation, parentheses are used to let perl know a bareword followed by parentheses is a function, for example:

CODE
foo();


from the print man page:

QUOTE
Also be careful not to follow the print keyword with a left parenthesis unless you want the corresponding right parenthesis to terminate the arguments to the print--interpose a + or put parentheses around all the arguments.


regular old print commands should just use quotes or quote operators:

CODE
print  "Exiting $script_name\n";
print qq{Exiting $script_name\n};


See the print man page for more details.

------------------------------

QUOTE
Thanks for the reply guys...but i'm still having trouble with how to pass a value from 1st Sub to 2nd..
I have modified my .pl to simplify...can you help me guide as why my call &End_Function($ORDER_RETURN_CODE) is not taking the assigned value 3 to the End_Function SUB?


CODE
sub Error_Routine
{
    $ORDER_RETURN_CODE = 3;
    &End_Function($ORDER_RETURN_CODE);
}


sub End_Function()
{
   my $ERR = @_;
     if ( $ERR == 3 )
    {
      exit 3;
    } else
     {
       print ( "\n");
       exit 0;
      }
}

User is offlineProfile CardPM
Go to the top of the page
+Quote Post

KevinADC
post 1 May, 2008 - 01:26 PM
Post #6


D.I.C Head

Group Icon
Joined: 23 Jan, 2007
Posts: 168

this is wrong:

my $ERR = @_;

when you assign a scalar the value of a list it will be the size of the list, not the contents of the list. Should be:

my ($ERR) = @_;

now you have assigned the value of @_ (assuming it is just one scalar you are passing) to the scalar, but in list context instead of scalar context.

or could be written all in scalar context:

my $ERR = $_[0];
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

siniks
post 2 May, 2008 - 03:51 PM
Post #7


New D.I.C Head

*
Joined: 30 Apr, 2008
Posts: 4

[quote name='KevinADC' date='1 May, 2008 - 01:26 PM' post='349683']
this is wrong:

my $ERR = @_;

when you assign a scalar the value of a list it will be the size of the list, not the contents of the list. Should be:

my ($ERR) = @_;

now you have assigned the value of @_ (assuming it is just one scalar you are passing) to the scalar, but in list context instead of scalar context.

or could be written all in scalar context:

my $ERR = $_[0];
[/quote]

[quote It works fine...thanks for correcting me Kavin!!. Appreciate your time and feedbacks. This is the first time i used this forum and am really impressed.
[/quote]

QUOTE
It works fine...thanks for correcting me Kavin!!. Appreciate your time and feedbacks. This is the first time i used this forum and am really impressed.

User is offlineProfile CardPM
Go to the top of the page
+Quote Post

KevinADC
post 2 May, 2008 - 05:39 PM
Post #8


D.I.C Head

Group Icon
Joined: 23 Jan, 2007
Posts: 168

You're welcome.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/16/08 10:31AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month