Why do you want classes instead of data structures? If all you want is an object with get/set methods, I don't see how that's all that different from a structure with getable/setable key-value pairs. An object would prevent the user from accidentally using some wrong name (i.e., it prevents typos), but otherwise you're just giving them a structure with more access overhead.

That said, generating a simple object from a hash is not that hard.

use strict; use warnings; sub hash2obj { my $hash_ref = shift; my $package = shift; bless $hash_ref => $package; foreach my $key ( keys %{$hash_ref} ) { no strict 'refs'; *{$package.'::get_'.$key} = sub { $_[0]->{$key} }; *{$package.'::set_'.$key} = sub { $_[0]->{$key} = $_[1] }; } return $hash_ref; } use Test::More 'tests' => 8; my $t1 = { a => 1, b => 2 }; hash2obj( $t1, 'Test::One' ); isa_ok( $t1, 'Test::One' ); can_ok( $t1, 'set_a' ); can_ok( $t1, 'set_b' ); can_ok( $t1, 'get_a' ); can_ok( $t1, 'get_b' ); is( $t1->{a}, $t1->get_a(), 'get_a same as $t1->{a}' ); $t1->set_a(3); is( $t1->get_a(), 3, 'a is 3 after set' ); is( $t1->{a}, $t1->get_a(), 'get_a same as $t1->{a} after set' );

This has some caveats:


In reply to Re: auto generating wrapper classes for complex types defined in WSDL by kyle
in thread auto generating wrapper classes for complex types defined in WSDL by Anonymous Monk

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.