Hi,

Let's say that I have a data structure from an external source, stored as a hash ref. For example
$external_structure = { 'name' => 'Test product', 'id' => 'ABC123', 'data' => { 'enabled' => '1', 'internal_id' => '', 'urls' => { 'main_url' => '' }, 'price' => '1000', 'unit' => 'ST', 'unit_info' => { 'qty' => '1', 'gtin' => '1234567890' } } };

Now, say that I want to map the fields to another structure (to be put in a database/textfile or so later on). I could do it in the following way:
my $internal_structure = {}; $internal_structure->{'product_id'} = $external_structure->{'id'}; $internal_structure->{'price_excl_vat'} = $external_structure->{'data' +}->{'price'}; $internal_structure->{'qty'} = $external_structure->{'data'}->{'unit_i +nfo'}->{'qty'}; # etc...
However, let's say that an end user wants to control the mappings, without having knowledge about or access to the source code. In theory, the mappings could be put in a textfile/CRM system or similar, like
product_id;id price_excl_vat;data#price qty;data#unit_info#qty
(There are of course better ways to do it, but just to explain the principle)

But, how can I then apply the mapping to the code? I have ideas on how to do it, but none of them seems elegant or foolproof...I would really like to be inspired on this one, so please hit me with your best ideas! There is no rule regarding how the mapping should look like, more than that is should be stored outside of the source code. And, there is no limit on how many levels the source structure has, so the solution can't be locked to a certain number of levels.

Also note that I unfortunately can't use any external module (like Data::Diver), I need a "core Perl"solution.

In reply to Mapping data structures through external source by DreamT

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.