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? |
Pass it like this and it should work:
$a = &SumSub($b,$c,$d); |
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.
|
Here is a test:
Code:
#!/usr/bin/perl Code:
rossy - trigger - Thu Feb 03 18:21:24 |
You can receive $d like this in SumSub:
Code:
$d = $_[scalar(@_)-1]; |
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. |
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? |
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... |
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. |