THRIFT-193: Patch for proper namespace support in perl compiler

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@728863 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 4d846c6..737dce9 100644
--- a/compiler/cpp/src/generate/t_perl_generator.cc
+++ b/compiler/cpp/src/generate/t_perl_generator.cc
@@ -184,6 +184,17 @@
     return result;
   }
 
+  std::string get_namespace_out_dir() {
+    std::string outdir = get_out_dir();
+    std::list<std::string> dirs;
+    perl_namespace_dirs(program_, dirs);
+    std::list<std::string>::iterator it;
+    for (it = dirs.begin(); it != dirs.end(); it++) {
+      outdir += *it + "/";
+    }
+    return outdir;
+  }
+
  private:
 
   /**
@@ -275,7 +286,7 @@
  * @param tenum The enumeration
  */
 void t_perl_generator::generate_enum(t_enum* tenum) {
-  f_types_ << "package "<<tenum->get_name()<<";"<<endl;
+  f_types_ << "package " << perl_namespace(program_) <<tenum->get_name()<<";"<<endl;
 
   vector<t_enum_value*> constants = tenum->get_constants();
   vector<t_enum_value*>::iterator c_iter;
@@ -448,6 +459,7 @@
   }
 
   //Create simple acessor methods
+  out << "use Class::Accessor;\n";
   out << "use base('Class::Accessor');\n";
 
   if (members.size() > 0) {
@@ -656,7 +668,7 @@
  * @param tservice The service definition
  */
 void t_perl_generator::generate_service(t_service* tservice) {
-  string f_service_name = get_out_dir()+service_name_+".pm";
+  string f_service_name = get_namespace_out_dir()+service_name_+".pm";
   f_service_.open(f_service_name.c_str());
 
   f_service_ <<
@@ -667,9 +679,10 @@
   f_service_ <<
     "use " << perl_namespace(tservice->get_program()) << "Types;" << endl;
 
-  if (tservice->get_extends() != NULL) {
+  t_service* extends_s = tservice->get_extends();
+  if (extends_s != NULL) {
     f_service_ <<
-      "use " << tservice->get_extends()->get_name() << ";" << endl;
+      "use " << perl_namespace(extends_s->get_program()) << extends_s->get_name() << ";" << endl;
   }
 
   f_service_ <<
@@ -699,8 +712,9 @@
 
   string extends = "";
   string extends_processor = "";
-  if (tservice->get_extends() != NULL) {
-    extends = tservice->get_extends()->get_name();
+  t_service* extends_s = tservice->get_extends();
+  if (extends_s != NULL) {
+    extends = perl_namespace(extends_s->get_program()) + extends_s->get_name();
     extends_processor = "use base('" + extends + "Processor');";
   }
 
@@ -708,7 +722,7 @@
 
   // Generate the header portion
   f_service_ <<
-      "package " << service_name_ << "Processor;" << endl << extends_processor << endl;
+      "package " << perl_namespace(program_) << service_name_ << "Processor;" << endl << extends_processor << endl;
 
 
   if (extends.empty()) {
@@ -937,15 +951,14 @@
  * @param tservice The service to generate a header definition for
  */
 void t_perl_generator::generate_service_interface(t_service* tservice) {
-  string extends = "";
   string extends_if = "";
-  if (tservice->get_extends() != NULL) {
-    extends = "use base('" + tservice->get_extends()->get_name() + "');";
-    extends_if = "use base('" + tservice->get_extends()->get_name() + "If');";
+  t_service* extends_s = tservice->get_extends();
+  if (extends_s != NULL) {
+    extends_if = "use base('" + perl_namespace(extends_s->get_program()) + extends_s->get_name() + "If');";
   }
 
   f_service_ <<
-    "package " << service_name_ << "If;"<<endl<<
+    "package " << perl_namespace(program_) << service_name_ << "If;"<<endl<<
     extends_if<<endl;
 
 
@@ -966,12 +979,13 @@
 void t_perl_generator::generate_service_rest(t_service* tservice) {
   string extends = "";
   string extends_if = "";
-  if (tservice->get_extends() != NULL) {
-    extends    =  tservice->get_extends()->get_name();
-    extends_if = "use base('" + tservice->get_extends()->get_name() + "Rest');";
+  t_service* extends_s = tservice->get_extends();
+  if (extends_s != NULL) {
+    extends    =  extends_s->get_name();
+    extends_if = "use base('" + perl_namespace(extends_s->get_program()) + extends_s->get_name() + "Rest');";
   }
   f_service_ <<
-    "package " << service_name_ << "Rest;"<<endl<<
+    "package " << perl_namespace(program_) << service_name_ << "Rest;"<<endl<<
     extends_if << endl;
 
 
@@ -1037,17 +1051,18 @@
 void t_perl_generator::generate_service_client(t_service* tservice) {
   string extends = "";
   string extends_client = "";
-  if (tservice->get_extends() != NULL) {
-    extends = tservice->get_extends()->get_name();
+  t_service* extends_s = tservice->get_extends();
+  if (extends_s != NULL) {
+    extends = perl_namespace(extends_s->get_program()) + extends_s->get_name();
     extends_client = "use base('" + extends + "Client');";
   }
 
   f_service_ <<
-      "package " << service_name_ << "Client;"<<endl;
+      "package " << perl_namespace(program_) << service_name_ << "Client;"<<endl;
 
   f_service_ <<
       extends_client << endl <<
-      "use base('" << service_name_ << "If');" << endl;
+      "use base('" << perl_namespace(program_) << service_name_ << "If');" << endl;
 
   // Constructor function
   f_service_ << "sub new {"<<endl;
diff --git a/test/perl/Makefile b/test/perl/Makefile
index 5703c3c..2ca39e2 100644
--- a/test/perl/Makefile
+++ b/test/perl/Makefile
@@ -10,7 +10,7 @@
 THRIFT = ../../compiler/cpp/thrift
 
 all: ../ThriftTest.thrift
-	$(THRIFT) -perl ../ThriftTest.thrift
+	$(THRIFT) --gen perl ../ThriftTest.thrift
 
 clean:
 	$(RM) -r gen-perl
diff --git a/test/perl/TestClient.pl b/test/perl/TestClient.pl
index 82e8320..ef5ff91 100644
--- a/test/perl/TestClient.pl
+++ b/test/perl/TestClient.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
 
 require 5.6.0;
 use strict;
@@ -7,14 +7,14 @@
 use Time::HiRes qw(gettimeofday);
 
 use lib '../../lib/perl/lib';
-use lib '../gen-perl';
+use lib 'gen-perl';
 
 use Thrift;
 use Thrift::BinaryProtocol;
 use Thrift::Socket;
 use Thrift::BufferedTransport;
 
-use ThriftTest;
+use ThriftTest::ThriftTest;
 use ThriftTest::Types;
 
 $|++;
@@ -28,7 +28,7 @@
 my $bufferedSocket = new Thrift::BufferedTransport($socket, 1024, 1024);
 my $transport = $bufferedSocket;
 my $protocol = new Thrift::BinaryProtocol($transport);
-my $testClient = new ThriftTestClient($protocol);
+my $testClient = new ThriftTest::ThriftTestClient($protocol);
 
 eval{
 $transport->open();
@@ -177,23 +177,23 @@
 # ENUM TEST
 #
 print("testEnum(ONE)");
-my $ret = $testClient->testEnum(Numberz::ONE);
+my $ret = $testClient->testEnum(ThriftTest::Numberz::ONE);
 print(" = $ret\n");
 
 print("testEnum(TWO)");
-$ret = $testClient->testEnum(Numberz::TWO);
+$ret = $testClient->testEnum(ThriftTest::Numberz::TWO);
 print(" = $ret\n");
 
 print("testEnum(THREE)");
-$ret = $testClient->testEnum(Numberz::THREE);
+$ret = $testClient->testEnum(ThriftTest::Numberz::THREE);
 print(" = $ret\n");
 
 print("testEnum(FIVE)");
-$ret = $testClient->testEnum(Numberz::FIVE);
+$ret = $testClient->testEnum(ThriftTest::Numberz::FIVE);
 print(" = $ret\n");
 
 print("testEnum(EIGHT)");
-$ret = $testClient->testEnum(Numberz::EIGHT);
+$ret = $testClient->testEnum(ThriftTest::Numberz::EIGHT);
 print(" = $ret\n");
 
 #
@@ -222,7 +222,7 @@
 # INSANITY TEST
 #
 my $insane = new ThriftTest::Insanity();
-$insane->{userMap}->{Numberz::FIVE} = 5000;
+$insane->{userMap}->{ThriftTest::Numberz::FIVE} = 5000;
 my $truck = new ThriftTest::Xtruct();
 $truck->string_thing("Truck");
 $truck->byte_thing(8);
diff --git a/tutorial/perl/PerlClient.pl b/tutorial/perl/PerlClient.pl
index 44a5a52..9451a94 100644
--- a/tutorial/perl/PerlClient.pl
+++ b/tutorial/perl/PerlClient.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
 
 use strict;
 use warnings;
@@ -11,8 +11,8 @@
 use Thrift::Socket;
 use Thrift::BufferedTransport;
 
-use SharedService;
-use Calculator;
+use shared::SharedService;
+use tutorial::Calculator;
 use shared::Types;
 use tutorial::Types;
 
@@ -21,7 +21,7 @@
 my $socket    = new Thrift::Socket('localhost',9090);
 my $transport = new Thrift::BufferedTransport($socket,1024,1024);
 my $protocol  = new Thrift::BinaryProtocol($transport);
-my $client    = new CalculatorClient($protocol);
+my $client    = new tutorial::CalculatorClient($protocol);
 
 
 eval{
@@ -36,7 +36,7 @@
 
     my $work = new tutorial::Work();
 
-    $work->op(Operation::DIVIDE);
+    $work->op(tutorial::Operation::DIVIDE);
     $work->num1(1);
     $work->num2(0);
 
@@ -47,7 +47,7 @@
         warn "InvalidOperation: ".Dumper($@);
     }
 
-    $work->op(Operation::SUBTRACT);
+    $work->op(tutorial::Operation::SUBTRACT);
     $work->num1(15);
     $work->num2(10);
     my $diff = $client->calculate(1, $work);