469,645 Members | 1,258 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

Call sub with substiuted args

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

Jul 19 '05 #1
1 1724
Rusty wrote:
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);

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Vedran Furac | last post: by
1 post views Thread by Thai Mai Shu | last post: by
4 posts views Thread by Crutcher | last post: by
18 posts views Thread by AlexanderVX | last post: by
44 posts views Thread by Steven D'Aprano | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.