<?xml version="1.0" encoding="windows-1252"?>
<node id="79629" title="Optimizing variable passing (code, peer review)" created="2001-05-11 02:09:00" updated="2005-07-25 05:53:11">
<type id="115">
perlquestion</type>
<author id="47597">
deprecated</author>
<data>
<field name="doctext">
&lt;code&gt;
sub simple_properties {
  my %properties = %{ shift() };
  my ($emissive, $diffuse, $shininess, $specular, $transp);
  foreach my $property (keys %properties) {
    if ($property eq "emissive") {
      $emissive = "emissiveColor  @{$properties{ $property }} ";
    }
    elsif ($property eq "diffuse") {
      $diffuse = "diffuseColor  @{$properties{ $property }} ";
    }
    elsif ($property eq "shininess") {
      $shininess = "shininess  @{$properties{ $property }} ";
    }
    elsif ($property eq "specular") {
      $specular = "specularColor  @{$properties{ $property }} ";
    }
    elsif ($property eq "transparency") {
      $transp = "transparency  @{$properties{ $property }} ";
    }
  }
  return &lt;&lt;"EOR";
appearance Appearance {
  material Material {
    $emissive
    $diffuse
    $shininess
    $specular
    $transp
  }
}
EOR
}
&lt;/code&gt;
Some of you may recognize my [http://www.vrml.org/|VRML] endeavours from my [deprecated|home node]. The above code does in fact work. And it's even pretty clear. However, every once in a while a great [japhy|saint] will come along and give me some suggestions that remarkably clarify what I am doing. So here are some things i'd like to clear up:
&lt;ul&gt;
&lt;li&gt;I don't like the creation of &lt;code&gt;%properties&lt;/code&gt; but I dont see a real good way to do it other than how its being done.
&lt;li&gt;I also dislike the use of separate strings for interpolation of the arrayrefs. However, I see them as being necessary for understanding the return value. Of course, there is more than one way to do it. I'm hoping somebody can come up with a way that is both simple and clean.
&lt;/ul&gt;
Here's some more code.
&lt;code&gt;
sub directional_light {
  my ($amb_intensity, $color_r, $direction_r, $intensity, $on) = (@_);
  my @color = @{ $color_r };
  my @direction = @{ $direction_r };
  $on = etf( $on );

  return &lt;&lt;"EOR";
DirectionalLight {
  ambientIntensity $amb_intensity
  color @color
  direction @direction
  intensity $intensity
  on $on
}
EOR
}
&lt;/code&gt;
This part kind of irritates me as well. The heredoc here is much clearer. What bothers me though is the creation of the arrays using sort of 'placeholder' values (which [japhy] understandably hates). The only way I can see to work around this is :
&lt;code&gt;
my (@array, @other_array) = (@{$_[0]}, @{$_[1]});
&lt;/code&gt;
which I find rather ugly and not particularly clear.
&lt;p&gt;
Anyhow, I'm particularly exhausted after a long day of hacking at the forthcoming (hopefully) VRML.pm. I hope my fellow monks can shed some light on this and save me a little time in refactoring.
&lt;p&gt;
yours truly,&lt;br&gt;
[deprecated|el deppo]
&lt;p&gt;--
&lt;br&gt;Laziness, Impatience, Hubris, and &lt;i&gt;Generosity&lt;/i&gt;.</field>
</data>
</node>
