Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-26 03:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found