Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you use a variable from 1 SubRoutine to be used in another?

Status
Not open for further replies.

Newbee21369

Programmer
Oct 13, 2004
30
0
0
US
I was trying to use a variable that was returned in Subroutine 1 to be used in Subroutine 2? Does anyone know how this can be done?
Thanks in Advance!
 
There are many ways to skin this cat.
The obvious I guess is to create a global variable.
Create a variable using "our $variablename;" at the top of your code, make sub1 set the value and you can then see that variable in sub2.


Trojan.

 
$var1,$var2 = Variables coming from subroutine 1

routine_number_2($var1,$var2);

sub routine_number_2 {
my ($var1,$var2) =@_;
# or
my @values) = @_;
#do stuff....
}

Example

sendmail ($gz, $gzbase, $mheader, $email);

sub sendmail {
my ($filename, $filelabel, $mailsubject, $email) = @_;
}

dmazzini
GSM System and Telecomm Consultant

 
The conventional way of sharing data like this between subroutines or functions is simply to pass the return value from sub1 as a param to sub2.

Code:
my $result = sub1();
sub2($result);

Trojan.
 
I tried what you suggested but I was still getting errors:
here is my code:

I'm trying pass the value of the variable "$FILE_NM" returned in the subroutine "get_file" to
the subroutine "Update_T_Received"? I'm needing this value to for my update statement. Thanks!!!

sub get_file
{

connect_dev_app();
$out_File_Ref_Cursor;

$sql = qq(
BEGIN
package.procedure(TO_DATE:)p1,:fmt),TO_DATE:)p2,:fmt ),:eek:ut_File_Ref_Cursor);
END;
);

$func = $dbh -> prepare($sql);

$func->bind_param(":p1",$starttimeStamp);
$func->bind_param(":p2",$endtimeStamp);
$func ->bind_param(":fmt","mm/dd/yyyy hh24:mi:ss");
$func ->bind_param_inout(":eek:ut_File_Ref_Cursor", \$out_File_Ref_Cursor, 0,{ora_type=>ORA_RSET});
$func->execute;

# Loop through Cursor Returned from package.procedure

while (($FILE_NM,$RECV_FILE_TS,$LAST_ATTEMPT_TS,$INB_OUT,$LOGICAL_NM) = $out_File_Ref_Cursor->fetchrow_array)
{
(LOGFILE, "SP returned FILE_NM: " . $FILE_NM . "\n");
}
$func->finish;

}


sub Update_T_Received
{

$updatestmt ="UPDATE T_Received SET CMPLT_TS =SYSDATE WHERE FILE_NM = '$FILE_NM'";
$dbh->do($updatestmt) || warn "prepare: $updatestmt: $DBI::errstr";
$dbh->disconnect();

}


 
What about:

Update_T_Received($FILE_NM);

sub Update_T_Received
{
my ($FILE_NM)=$_;
$updatestmt ="UPDATE T_Received SET CMPLT_TS =SYSDATE WHERE FILE_NM = '$FILE_NM'";
$dbh->do($updatestmt) || warn "prepare: $updatestmt: $DBI::errstr";
$dbh->disconnect();

}




dmazzini
GSM System and Telecomm Consultant

 
I got the error that I did not have enough parameters defined when I tried that.
thanks for your input.
 
I don't see what's the problem. It does not seem to be problems passing the variable...Can you print it and don't execute the update statement.

Something like:

$FILE_NM="HOLA";

Update_T_Received($FILE_NM);

sub Update_T_Received
{
my ($FILE_NM)=$_;
print "HELLO $FILE_NM\n";

}

dmazzini
GSM System and Telecomm Consultant

 
Yes,this works when $FILE_NM is not contained in another subroutine.
 
first_routine();


sub first_routine{
$FILE_NM="HOLA";
Update_T_Received($FILE_NM);
}




sub Update_T_Received {
$FILE_NM=$_[0];
print "MYVARIABLE:$FILE_NM\n";
}

dmazzini
GSM System and Telecomm Consultant

 
I tried to call Update_T_Received();
but it prints: MYVARIABLE:
is there something more I need to do?
 
Here is a more simplified version of code to see what I'm trying to accomplish. I want it to print the following:
HOLA means Hello in Spanish, and we say HELLO in English.
silly print statement, but I'm trying to show you want I'm trying to accomplish. Right now it prints nothing.
Thanks Again.


#!/usr/bin/perl -w

our $SPANISH;

sub first_routine
{
$SPANISH="HOLA";
return $SPANISH;
}


sub second_routine
{
my $ENGLISH ="HELLO";
return $ENGLISH;
print "$SPANISH means Hello in Spanish, and we say $ENGLISH in English\n";
}


second_routine();
 
A couple of suggestions, based on the SPANISH example:

1) Return exits your subroutine. Eliminate the return $ENGLISH from second_subroutine, and it should run correctly.

2) You are not calling first_routine in this example. Are you calling both get_file and Update_T_Received in the original program?
 
Your right, I have to call the first sub to get the value for $SPANISH which is needed for the second sub.
currently I'm getting the print statement:
means Hello in Spanish, and we say HELLO in English

Any suggestions would be greatly appreciated.
Here is my modified code:

#!/usr/bin/perl -w

our $SPANISH;

sub first_routine
{
$SPANISH="HOLA";
return $SPANISH;
}


sub second_routine
{
my $ENGLISH ="HELLO";
print "$SPANISH means Hello in Spanish, and we say $ENGLISH in English\n";
}

sub first_routine();
second_routine();
 
Try it:


sub first_routine
{
$SPANISH="HOLA";
return $SPANISH;
}


sub second_routine
{
my $ENGLISH ="HELLO";
print "$SPANISH means Hello in Spanish, and we say $ENGLISH in English\n";
}

first_routine();
second_routine();


The problem was:

sub first_routine();
second_routine();

dmazzini
GSM System and Telecomm Consultant

 
Haha yes that would make quite a difference!! Thanks for your help!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top