title: Wrapper generator SWIG (C++/Perl): How to access "blessed" objects in a 1d vector<double>in Perl?
body: <p>I have written a C++ library to extract simulation data (= simple vectors with (x,y) or (x,y,z) components) from electronic design automation (EDA) tools. More concrete, this data represents electrical signals for different points in time.</p>
  <p>The C++ library offers several methods. Two important ones are:</p>
  <pre><code>std::vector&lt;std::string&gt; getSignalNames() // Returns all signal names in the file
  std::vector&lt;std::vector&lt;double&gt;&gt; getSignals() // Returns the actual data as M x N matrix (with M rows and N columns)
  </code></pre>
  <p>Using the library in C++ works perfectly and yields the expected results, e.g.:</p>
  <pre><code>getSignalNames():
    Signal1
    Signal2
  getSignals():
    1 1 1 2
    2 1 2 3
  </code></pre>
  <p>Perl programmers asked me to also offer the library to them and I decided to use <a href="http://www.swig.org/index.php" rel="nofollow noreferrer">the wrapper generator SWIG</a> to create bindings. I worked through <a href="http://www.swig.org/tutorial.html" rel="nofollow noreferrer">the tutorial</a> and I was able to successfully set up a minimal working example.</p>
  <p>Based on the example, I wrote a complete SWIG interface file for the C++ library. The wrapper generation and build process works smoothly and I can also use <code>getSignalNames()</code> without any problems:</p>
  <pre><code>// Perl snippet to read out signal names
  my $parserPointer = new waveformparser::ScopeParser(&quot;input.file&quot;);
  $signalNames = $parserPointer-&gt;getSignalNames();
  
  foreach my $signalName ( @$signalNames ) {
      print &quot;$signalName\n&quot;;
  }
  // Output:
  Signal1
  Signal2
  </code></pre>
  <p>But, I ran into trouble when using the return value from <code>getSignals()</code>:</p>
  <pre><code>// Perl snippet to read out the actual signal data
  my $parserPointer = new waveformparser::ScopeParser(&quot;input.file&quot;);
  $signalData = $parserPointer-&gt;getSignals();
  
  foreach my $rowAsHashRef ( @$signalData ) {
      print &quot;reftype: &quot; . reftype($rowAsHashRef) . &quot;\n&quot;;
      print &quot;keys: &quot; . keys(%$rowAsHashRef) . &quot;\n&quot;
  }
  // Output:
  reftype: HASH
  keys: 0
  reftype: HASH
  keys: 0
  </code></pre>
  <p>As you see, each row is represented as hash in Perl, but there are no keys in the Hash. Nevertheless, when using <a href="https://metacpan.org/pod/Data::Dumper" rel="nofollow noreferrer">Perl's Data::Dumper</a>, I can see the correct data type for each row:</p>
  <pre><code>my $parserPointer = new waveformparser::ScopeParser(&quot;input.file&quot;);
  $signalData = $parserPointer-&gt;getSignals();
  print Dumper $signalData;
  // Output:
  $VAR1 = [
            bless( {}, 'waveformparser::vector_1d_double' ),
            bless( {}, 'waveformparser::vector_1d_double' )
  ];
  </code></pre>
  <p>I.e., according to the data dumper, each row consists of several columns (i.e., <code>'waveformparser::vector_1d_double'</code>) which are defined in the SWIG interface file as following:</p>
  <pre><code>...
  %include &quot;std_vector.i&quot;
  %template(vector_1d_double) std::vector&lt;double&gt;;
  %template(vector_2d_double) std::vector&lt;std::vector&lt;double&gt;&gt;;
  ...
  </code></pre>
  <p><strong>My question is now: How can I access elements of this &quot;blessed&quot; (wrapped) <code>vector_1d_double</code> objects in Perl?</strong></p>
  <p>I thought, SWIG would provide convenient access methods for such objects. I.e., the underlying C++ data type is just a simple 1d vector of doubles (<code>std::vector&lt;double&gt;</code>).</p>