LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How Can I Pass ($b,%c,$d) to a Perl Sub (https://www.linuxquestions.org/questions/programming-9/how-can-i-pass-%24b-c-%24d-to-a-perl-sub-285761/)

benpedersen 02-03-2005 08:22 AM

How Can I Pass ($b,%c,$d) to a Perl Sub
 
Call:
$a = &SumSub($b,%c,$d);

Sub:
sub SumSub{
# Make Passed Prams Available To SumSub
my ($b,%c,$d) = (@_);

The above is not working. I never receive "$d" in SumSub. Unfortunately I can not "$c = \%c;" because I can not go and change the existing SumSub calls to $c (to many).

Is there any way I can get $d into SumSub even when passing a Hash before it?

david_ross 02-03-2005 11:46 AM

Pass it like this and it should work:
$a = &SumSub($b,$c,$d);

benpedersen 02-03-2005 11:58 AM

I cant send $c because in the other 500 locations that SumSub is called it already uses %c. If I would have written the code I would have referenced the Hash passing $c. But I have to use what is already there.

david_ross 02-03-2005 12:20 PM

Here is a test:
Code:

#!/usr/bin/perl

$b="bee";
$c{'one'}=1;
$c{'two'}=2;
$d="dee";

$a = &SumSub($b,$c,$d);

sub SumSub{
# Make Passed Prams Available To SubSub
my ($b,$c,$d) = (@_);

print <<"EOF";

b=$b
c-one=$c{'one'}
c-two=$c{'two'}
d=$d

EOF
}

exit;

It returns:
Code:

rossy - trigger - Thu Feb 03 18:21:24
~> perl test.pl

b=bee
c-one=1
c-two=2
d=dee


sasho 02-03-2005 12:25 PM

You can receive $d like this in SumSub:

Code:

$d = $_[scalar(@_)-1];

benpedersen 02-04-2005 01:25 PM

david_ross,
unfortunately I can not change how sumsub is called. I can not make it $c because in the 500 places the subsub is called it passes %c. I can only make the change from the inside of sumsub.

sasho,
I do receive $d using what you suggested, however when I use

$d = $_[scalar(@_)-1]; or $d = $_[2];

I no longer receive $b nor %c. I don't know why.

Thank you both. If you can think of anything, let me know.

sasho 02-04-2005 07:26 PM

Hmm I don't know what else to tell you... Either send a reference to the hash, rather than the whole %c, or make $d global, and work that way...

Are you forbidden from modifying the code to that sub? Search and replace will not do?

chrism01 02-06-2005 10:55 PM

It's not quite clear how this sub is already being called, but you could use pop/shift as defined here: http://search.cpan.org/~nwclark/perl...erl_Functions,
the only problem is knowing when to do this and when not, if the current 500 calls do not pass the last param value ...
Possibly check if scalar(@_) is odd or even...

benpedersen 02-23-2005 03:44 PM

sasho, I work in a LARGE global company that requires a large amount of paperwork for each program changed(even if it is a minor change) that is why I don't change %c to $c.

Making $d global would work but I didn't want to do that. I have decided to just pass $d within %c (a little better than making $d global).

Sub:
sub SumSub{
# Make Passed Prams Available
my ($b,%c) = (@_);

$d = $c{$d};

Thank you all for the help.


All times are GMT -5. The time now is 03:15 AM.