Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
You can probably simplify the top by doing the following:
my %property_hash = ( emmisive => 'emmisiveColor', diffuse => 'diffuseColor', shineness => 'shineness', speculary => 'specularColor', transparency => 'transparency' }; my $prop_string; foreach my $key ( keys %property_hash ) { # Mark! $prop_string .= $key . ' ' . @{$properties{ $key }} . "\n" if $properties{ $key }; } return <<"EOR"; appearance Appearance { material Material { $prop_string } } EOR
Note that if you need defaults, you could also define these with the same keys as above in %prop_def, and then the marked line becomes:
$prop_string .= $key . ' ' . ( @{$properties{ $key }} || @{$prop_def{ $key }} ) . "\n"

Update on your second part. Unfortunately, getting the values out of @_ gets messy fast. Some other possible options:

Use a hash to pass values in. This allows not only for later expansion without breaking the routines, but also for default values on your end. However, it's hard to do any compile-time checking for right parameters. EG:

sub code { my %hash = %{ shift() }; my $value = $hash{ 'expected' } || $const_def_hash{ 'expected' }; }

Taking a cue from the example above, use defencing of shifts to get your variables without the extra variable. You can still do some compile-time checking, but again, if you need to update the code, you might break other users code.

sub code { # scalar, array ref, hash ref my $data = shift; my @array = @{ shift() }; my %hash = %{ shift() }; }

Since all you seem to be doing is taking arguements and using that to pump out a text file for VRML, without doing any major processing of the code, you *may* want to consider using somelike like Template Toolkit 2, along with the hash argument suggestion, to make it easier to keep the perl code and VRML generatation separate.

Note that I can see a nice way to tree all your data, and use a single call to TT2 to get everything out in one go, but that's probably beyond the scope of the help you are requesting.

Update 2 - fixed an if condition above, thanks chipmunk!


Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

In reply to Re: Optimizing variable passing (code, peer review) by Masem
in thread Optimizing variable passing (code, peer review) by deprecated

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (9)
    As of 2014-10-02 17:44 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      What is your favourite meta-syntactic variable name?














      Results (67 votes), past polls