Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Optimizing variable passing (code, peer review)

by Masem (Monsignor)
on May 11, 2001 at 07:15 UTC ( #79635=note: print w/replies, xml ) Need Help??

in reply to Optimizing variable passing (code, peer review)

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 - || "You've left the lens cap of your mind on again, Pinky" - The Brain

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://79635]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2021-09-27 08:40 GMT
Find Nodes?
    Voting Booth?

    No recent polls found