Call sub with substiuted args

R

Rusty

Greetings,

My problem is I need to call a subroutine but the number of args can be
many and vary from call to call. What I'm trying to do is parse a
template and capture the data from the template into a scalar and then
call the subroutine with the scalar value and have perl substitute the
scalar with the data in the scalar and have that data interpreted
correctly. Here's some code to illustrate:

#!/usr/bin/perl

my $x = q/-values => {K1 => V1, K2 => V2}/;
mytestsub($x);
exit;

Now the above works, but not the way I hoped. It sees a simple scalar
sent to the subroutine. What I'm shooting for is this:

#!/usr/bin/perl

mytestsub(-values => {K1 => V1, K2 => V2, ...});
exit;

The snippet above the subroutine would see an array that contains a
scalar and a hash.

Has anyone had any success in doing this? I believe I can get type of
thing under tcl but that's not an option.

I can do this a couple other ways but I was hoping for this as it would
save me lots of pain :)

Thanks in advance for any tips.

Rusty
 
G

Gunnar Hjalmarsson

Rusty said:
What I'm shooting for is this:

#!/usr/bin/perl

mytestsub(-values => {K1 => V1, K2 => V2, ...});
exit;

The snippet above the subroutine would see an array that contains a
scalar and a hash.

Sounds as if you want to pass a scalar and a hash reference to the sub.

my $scalar = '-values';
my $hashref = { K1 => 'V1', K2 => 'V2' };

mytestsub($scalar, $hashref);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,573
Members
45,046
Latest member
Gavizuho

Latest Threads

Top