LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-03-2005, 08:22 AM   #1
benpedersen
LQ Newbie
 
Registered: Feb 2005
Location: Ann Arbor Michigan
Posts: 4

Rep: Reputation: 0
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?

Last edited by benpedersen; 02-04-2005 at 01:33 PM.
 
Old 02-03-2005, 11:46 AM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
Pass it like this and it should work:
$a = &SumSub($b,$c,$d);
 
Old 02-03-2005, 11:58 AM   #3
benpedersen
LQ Newbie
 
Registered: Feb 2005
Location: Ann Arbor Michigan
Posts: 4

Original Poster
Rep: Reputation: 0
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.
 
Old 02-03-2005, 12:20 PM   #4
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
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
 
Old 02-03-2005, 12:25 PM   #5
sasho
Member
 
Registered: Jan 2005
Distribution: Arch
Posts: 120

Rep: Reputation: 17
You can receive $d like this in SumSub:

Code:
$d = $_[scalar(@_)-1];
 
Old 02-04-2005, 01:25 PM   #6
benpedersen
LQ Newbie
 
Registered: Feb 2005
Location: Ann Arbor Michigan
Posts: 4

Original Poster
Rep: Reputation: 0
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.

Last edited by benpedersen; 02-04-2005 at 01:32 PM.
 
Old 02-04-2005, 07:26 PM   #7
sasho
Member
 
Registered: Jan 2005
Distribution: Arch
Posts: 120

Rep: Reputation: 17
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?
 
Old 02-06-2005, 10:55 PM   #8
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,225

Rep: Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021
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...
 
Old 02-23-2005, 03:44 PM   #9
benpedersen
LQ Newbie
 
Registered: Feb 2005
Location: Ann Arbor Michigan
Posts: 4

Original Poster
Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with perl module for w3c validator to work on my local Apache+PHP+perl instal tbamt Linux - Software 0 12-16-2004 05:37 PM
Yum error: .conflict between perl and perl-NDBM_File zepplin611 Red Hat 3 10-20-2004 10:22 AM
Hiding code in PERL, perl gui question randomx Programming 1 06-26-2004 03:22 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
chrooting apache v2 (php, ssl, perl support) ; perl configuration markus1982 Linux - Security 3 01-26-2003 06:15 PM


All times are GMT -5. The time now is 08:50 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration