Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Best Data Structure for passing in multiple arguments to a sub-routine

by ~~David~~ (Hermit)
on Mar 13, 2007 at 15:39 UTC ( #604580=perlquestion: print w/replies, xml ) Need Help??

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

Right now, I have a long, and non-reusable peice of code that builds a power point presentation for me. I was thinking about how I can make it better so that as my project evolves / changes, I have to do minimal re-writing.

I would like to make a subroutine that takes certain arguments, and creates my slides for me, for instance, I would like to be able to pass something like this:

1. SLIDE #1, TYPE = ppBLAHBLAH, Title = ... a. Element #1: 1. Type = picture 2. path to picture 3. parameter 1, etc.. b. Element #2: 1. type = text box 2. text... 3. parameter, etc...

What is the best data structure to use to accomplish this goal? Can I make this with a hash of hashes?

~~David~~
  • Comment on Best Data Structure for passing in multiple arguments to a sub-routine
  • Download Code

Replies are listed 'Best First'.
Re: Best Data Structure for passing in multiple arguments to a sub-routine
by philcrow (Priest) on Mar 13, 2007 at 15:45 UTC
    Since you care about the order you probably want a hash with keys for type, title, and elements which would hold an array reference of hashes which describe child elements. your parameter list (3. above) might also be an array.

    Something like this:

    make_slide( { title => 'Introduction to Slide Making', type => 'elemental', elements => [ { type => 'pic', path => '...', ... }, { type => 'text box', text => '...' }, ], } );

    In particular, I frequently use type coded sub hashes and if test on their types to control how the other args are treated.

    Phil

Re: Best Data Structure for passing in multiple arguments to a sub-routine
by clinton (Priest) on Mar 13, 2007 at 17:32 UTC
    To make it easier to read and to maintain (I presume this is going to be some sort of template), it may be easiest to represent the data in YAML (YAML::Syck for speed). For some elements, you may need hashes, for others array - writing it down in YAML notation makes it easy to adjust your data structure to suit the need.

    For instance, you could represent your example as follows:

    slides: - no: 1 type: ppBLAHBLAH title: My title elements: - type: picture path: /my/path width: 100 height: 50 - type: text box text: | A bunch of flowing text style: italics
Re: Best Data Structure for passing in multiple arguments to a sub-routine
by planetscape (Chancellor) on Mar 13, 2007 at 20:33 UTC
Re: Best Data Structure for passing in multiple arguments to a sub-routine
by imp (Priest) on Mar 13, 2007 at 15:44 UTC
    I would go with the hash of hashes usually, or objects that encapsulates the different bits if it is complicated.
Re: Best Data Structure for passing in multiple arguments to a sub-routine
by GrandFather (Saint) on Mar 13, 2007 at 19:47 UTC

    The absolute without question best structure to use is a reference - to something.

    Maybe the structure isn't important at all? Maybe you should be looking at is something like Text::Template?

    Where is the text going to come from that describes the elements? Maybe what you really ought to use is Parse::RecDescent?


    DWIM is Perl's answer to Gödel
Re: Best Data Structure for passing in multiple arguments to a sub-routine
by jettero (Monsignor) on Mar 13, 2007 at 15:42 UTC

    I think answers to this are likey to be mostly subjective. Since the LHS appears to always be numbered and never named, it feels like an list of lists to me.

    -Paul

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://604580]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2022-11-30 20:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?