OO perl accessors for Thrift objects

Summary: Submitted by Jake Luciani

Reviewed By: cpiro

Test Plan: Supplied in test/tutorial code


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665276 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/generate/t_perl_generator.cc b/compiler/cpp/src/generate/t_perl_generator.cc
index ed3fab7..76a4443 100644
--- a/compiler/cpp/src/generate/t_perl_generator.cc
+++ b/compiler/cpp/src/generate/t_perl_generator.cc
@@ -256,6 +256,23 @@
     out << "use base('Thrift::TException');\n";
   }
 
+  //Create simple acessor methods
+  out << "use base('Class::Accessor');\n";
+
+  if (members.size() > 0) {
+      out << perl_namespace(tstruct->get_program()) << tstruct->get_name() <<"->mk_accessors( qw( ";
+      for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+          t_type* t = get_true_type((*m_iter)->get_type());
+          if (!t->is_xception()) {
+              out << (*m_iter)->get_name() << " ";
+          }
+      }
+
+      out << ") );\n";
+  }
+
+
+  // new()
   out << "sub new {\n";
   indent_up();
   out << "my $classname = shift;\n";
diff --git a/lib/perl/README b/lib/perl/README
index d1eef70..3b95f13 100644
--- a/lib/perl/README
+++ b/lib/perl/README
@@ -1,7 +1,7 @@
 Thrift Perl Software Library
 
-Author: T Jake Luciani (jakers@gmail.com)
-Last Modified: 2007-Apr-28
+Author: T Jake Luciani (jake@3.rdrail.net)
+Last Modified: 2007-Sep-18
 
 Thrift is distributed under the Thrift open source software license.
 Please see the included LICENSE file.
@@ -17,22 +17,11 @@
 The 64bit Integers work only upto 2^42 on my machine :-?
 Math::BigInt is probably needed.
 
-The only other issue I have with this implementation is the lack of
-strict accessor methods, for example: to set a struct with variable
-foo you must assign it via hash key:
-
-my $x = new StructWithFoo();
-$x->{foo} = "bar";
-
-rather than:
-
-$x->foo("bar");
-
 Please see tutoral and test dirs for examples...
 
 Dependencies
 ============
 
-Bit::Vector - comes with modern perl installations.
-
+Bit::Vector     - comes with modern perl installations.
+Class::Accessor
 
diff --git a/test/perl/TestClient.pl b/test/perl/TestClient.pl
index 4e6278e..82e8320 100644
--- a/test/perl/TestClient.pl
+++ b/test/perl/TestClient.pl
@@ -83,33 +83,33 @@
 # STRUCT TEST
 #
 print("testStruct({\"Zero\", 1, -3, -5})");
-my $out = new Xtruct();
-$out->{string_thing} = "Zero";
-$out->{byte_thing} = 1;
-$out->{i32_thing} = -3;
-$out->{i64_thing} = -5;
+my $out = new ThriftTest::Xtruct();
+$out->string_thing("Zero");
+$out->byte_thing(1);
+$out->i32_thing(-3);
+$out->i64_thing(-5);
 my $in = $testClient->testStruct($out);
-print(" = {\"".$in->{string_thing}."\", ".
-        $in->{byte_thing}.", ".
-        $in->{i32_thing}.", ".
-        $in->{i64_thing}."}\n");
+print(" = {\"".$in->string_thing."\", ".
+        $in->byte_thing.", ".
+        $in->i32_thing.", ".
+        $in->i64_thing."}\n");
 
 #
 # NESTED STRUCT TEST
 #
 print("testNest({1, {\"Zero\", 1, -3, -5}, 5}");
-my $out2 = new Xtruct2();
-$out2->{byte_thing} = 1;
-$out2->{struct_thing} = $out;
-$out2->{i32_thing} = 5;
+my $out2 = new ThriftTest::Xtruct2();
+$out2->byte_thing(1);
+$out2->struct_thing($out);
+$out2->i32_thing(5);
 my $in2 = $testClient->testNest($out2);
-$in = $in2->{struct_thing};
-print(" = {".$in2->{byte_thing}.", {\"".
-      $in->{string_thing}."\", ".
-      $in->{byte_thing}.", ".
-      $in->{i32_thing}.", ".
-      $in->{i64_thing}."}, ".
-      $in2->{i32_thing}."}\n");
+$in = $in2->struct_thing;
+print(" = {".$in2->byte_thing.", {\"".
+      $in->string_thing."\", ".
+      $in->byte_thing.", ".
+      $in->i32_thing.", ".
+      $in->i64_thing."}, ".
+      $in2->i32_thing."}\n");
 
 #
 # MAP TEST
@@ -221,13 +221,13 @@
 #
 # INSANITY TEST
 #
-my $insane = new Insanity();
+my $insane = new ThriftTest::Insanity();
 $insane->{userMap}->{Numberz::FIVE} = 5000;
-my $truck = new Xtruct();
-$truck->{string_thing} = "Truck";
-$truck->{byte_thing} = 8;
-$truck->{i32_thing} = 8;
-$truck->{i64_thing} = 8;
+my $truck = new ThriftTest::Xtruct();
+$truck->string_thing("Truck");
+$truck->byte_thing(8);
+$truck->i32_thing(8);
+$truck->i64_thing(8);
 push(@{$insane->{xtructs}}, $truck);
 
 print("testInsanity()");
@@ -269,7 +269,7 @@
 eval {
     $testClient->testException('Xception');
     print("  void\nFAILURE\n");
-}; if($@ && $@->UNIVERSAL::isa('Xception')) {
+}; if($@ && $@->UNIVERSAL::isa('ThriftTest::Xception')) {
     print(' caught xception '.$@->{errorCode}.': '.$@->{message}."\n");
 }
 
diff --git a/tutorial/perl/PerlClient.pl b/tutorial/perl/PerlClient.pl
index 10cb725..44a5a52 100644
--- a/tutorial/perl/PerlClient.pl
+++ b/tutorial/perl/PerlClient.pl
@@ -31,14 +31,14 @@
     print "ping()\n";
 
 
-    my $sum = $client->add(1000000,1);
+    my $sum = $client->add(1,1);
     print "1+1=$sum\n";
 
-    my $work = new Work();
+    my $work = new tutorial::Work();
 
-    $work->{op} = Operation::DIVIDE;
-    $work->{num1} = 1;
-    $work->{num2} = 0;
+    $work->op(Operation::DIVIDE);
+    $work->num1(1);
+    $work->num2(0);
 
     eval {
         $client->calculate(1, $work);
@@ -47,9 +47,9 @@
         warn "InvalidOperation: ".Dumper($@);
     }
 
-    $work->{op} = Operation::SUBTRACT;
-    $work->{num1} = 15;
-    $work->{num2} = 10;
+    $work->op(Operation::SUBTRACT);
+    $work->num1(15);
+    $work->num2(10);
     my $diff = $client->calculate(1, $work);
     print "15-10=$diff\n";