elusion has asked for the wisdom of the Perl Monks concerning the following question:

Monks,
I don't know what to do, but it may just be my brain's not working today. I want to take a command line statement such as subname param1, param2, { param3.1, param3.2 }, getthepicture and turn it into subname(@params) where @params = ("param1", "param2", { "param3.1", "param3.2" }) without creating a set number of params. How do I do it?

- p u n k k i d
"Reality is merely an illusion, albeit a very persistent one." -Albert Einstein

Replies are listed 'Best First'.
Re: Command line to array
by MeowChow (Vicar) on Apr 26, 2001 at 04:32 UTC
Re: Command line to array
by jepri (Parson) on Apr 26, 2001 at 04:49 UTC
    MeowChow probably didn't suggest this one because it's horrible practise, but I think you get away with something like this provided that you didn't give a damn about security:

    my @list=('test', 'Hello!'); my $func_name=shift @list; &$func_name(@list); sub test { print @_; }

    To my suprise, this works fine.

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

        Merlyn, how bad is it to do:
        use strict; no strict 'refs';
        From your post it sounds as though you disapprove, is this so? I'm concerned as I use jump tables quite often. AFAIK this isn't possible without undoing strict refs. Is there another way of doing jump tables? Or a healthier alternative?

        --
        
        Brother Frankus.

        ¤

Re: Command line to array
by elusion (Curate) on Apr 26, 2001 at 04:22 UTC
    Ok, I've got more to add, sorry folks. $previous_writeup =~ s/Command line/STDIN/gi; Ok, here's some new and (hopefully) inmproved writeup material.

    I want to be able to run

    table({rows => '2'}, "Cell 1", "Cell", "Blah, blah");
    as
    prompt:) table {rows => '2'}, "Cell 1", "Cell", "Blah, blah"
    But I want to be able to do that with any subroutine. Examples of params could be as follows
    [ [ "Blah", "Blah" ], [ "Blah", "Blah" ] ] "Blah, Blah, Blah" [ [ "Baz", [ { Wow => "that's Nesting" }, "Bar" ], "foo" ], [ "More", "Too lazy to be creative", "More here" ] ]
    I hope that this make sense. Thank you for excusing my stupity today.

    - p u n k k i d
    "Reality is merely an illusion, albeit a very persistent one." -Albert Einstein

      eval is the easiest way to do this, since you don't end up with your own mini-language to parse; just let perl do it. Here's a very primitive command-line example:
      % perl -MData::Dumper -nwe 'chomp; ($sub, $rest) = split / +/, $_, 2; eval "$sub($rest)"; print $@ if $@; print "\n) " } BEGIN { sub spew { print Dumper(@_) } print ") "'
      Presumably you'll have more functions defined than that; consider using Term::Readline or similar if this is something that'll be used a lot.

      hdp.