Call sub with substiuted args

Discussion in 'Perl' started by Rusty, Dec 28, 2003.

  1. Rusty

    Rusty Guest

    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
     
    Rusty, Dec 28, 2003
    #1
    1. Advertising

  2. 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
     
    Gunnar Hjalmarsson, Dec 28, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Varn
    Replies:
    2
    Views:
    643
    Ken Varn
    Jun 22, 2005
  2. Replies:
    3
    Views:
    507
    David Eppstein
    Sep 17, 2003
  3. Pierre Fortin

    args v. *args passed to: os.path.join()

    Pierre Fortin, Sep 18, 2004, in forum: Python
    Replies:
    2
    Views:
    710
    Pierre Fortin
    Sep 18, 2004
  4. Ben
    Replies:
    2
    Views:
    920
  5. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,083
    Lawrence D'Oliveiro
    May 20, 2011
Loading...

Share This Page