Passing return value between sub routines in perl

Passing return value between sub routines in perl

Page 1 of 1

7 Replies - 4864 Views - Last Post: 02 May 2008 - 05:39 PM

#1 siniks   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-April 08

Passing return value between sub routines in perl

Post icon  Posted 30 April 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;
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: Passing return value between sub routines in perl

#2 fahlyn   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 03-November 07

Re: Passing return value between sub routines in perl

Posted 30 April 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.
Was This Post Helpful? 0
  • +
  • -

#3 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Passing return value between sub routines in perl

Posted 30 April 2008 - 11:49 PM

View Postfahlyn, on 30 Apr, 2008 - 05:46 PM, said:

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:

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.
Was This Post Helpful? 0
  • +
  • -

#4 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Passing return value between sub routines in perl

Posted 01 May 2008 - 12:06 AM

View Postsiniks, on 30 Apr, 2008 - 01:03 PM, said:

/* 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:

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:

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:

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:

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


See the print man page for more details.
Was This Post Helpful? 0
  • +
  • -

#5 siniks   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-April 08

Re: Passing return value between sub routines in perl

Posted 01 May 2008 - 01:14 PM

View PostKevinADC, on 1 May, 2008 - 12:06 AM, said:

View Postsiniks, on 30 Apr, 2008 - 01:03 PM, said:

/* 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:

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:

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:

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:

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?


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


Was This Post Helpful? 0
  • +
  • -

#6 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Passing return value between sub routines in perl

Posted 01 May 2008 - 01:26 PM

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];
Was This Post Helpful? 0
  • +
  • -

#7 siniks   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-April 08

Re: Passing return value between sub routines in perl

Posted 02 May 2008 - 03:51 PM

View PostKevinADC, on 1 May, 2008 - 01:26 PM, said:

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

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.

Was This Post Helpful? 0
  • +
  • -

#8 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Passing return value between sub routines in perl

Posted 02 May 2008 - 05:39 PM

You're welcome.
Was This Post Helpful? 1

Page 1 of 1