Allow nested namespaces to be used in perl.


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665584 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 5e1503b..0b16212 100644
--- a/compiler/cpp/src/generate/t_perl_generator.cc
+++ b/compiler/cpp/src/generate/t_perl_generator.cc
@@ -22,10 +22,12 @@
   MKDIR(get_out_dir().c_str());
 
   string outdir = get_out_dir();
-  std::string ns = program_->get_perl_package();
-  if (ns.length() > 0) {
-    outdir += ns + "/";
-    MKDIR(outdir.c_str());
+  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 + "/";
+      MKDIR(outdir.c_str());
   }
 
   // Make output file
@@ -42,7 +44,7 @@
   // Print header
   f_consts_ <<
       autogen_comment() <<
-      "package "<<( ns.empty() ? "" : ns+"::")<<"Constants;"<<endl<<
+      "package "<< perl_namespace(program_) <<"Constants;"<<endl<<
       perl_includes() <<
       endl;
 }
diff --git a/compiler/cpp/src/generate/t_perl_generator.h b/compiler/cpp/src/generate/t_perl_generator.h
index 689cf1c..fc10704 100644
--- a/compiler/cpp/src/generate/t_perl_generator.h
+++ b/compiler/cpp/src/generate/t_perl_generator.h
@@ -11,6 +11,7 @@
 #include <fstream>
 #include <iostream>
 #include <vector>
+#include <list>
 
 #include "t_oop_generator.h"
 
@@ -141,9 +142,40 @@
       "#\n";
   }
 
+  void perl_namespace_dirs(t_program* p, std::list<std::string>& dirs) {
+      std::string ns = p->get_perl_package();
+      std::string::size_type loc;
+
+      if (ns.size() > 0) {
+        while ((loc = ns.find(".")) != std::string::npos) {
+          dirs.push_back(ns.substr(0, loc));
+          ns = ns.substr(loc+1);
+        }
+      }
+
+      if (ns.size() > 0) {
+          dirs.push_back(ns);
+      }
+  }
+
   std::string perl_namespace(t_program* p) {
     std::string ns = p->get_perl_package();
-    return ns.empty() ? ns : (ns + "::");
+    std::string result = "";
+    std::string::size_type loc;
+
+    if (ns.size() > 0) {
+      while ((loc = ns.find(".")) != std::string::npos) {
+        result += ns.substr(0, loc);
+        result += "::";
+        ns = ns.substr(loc+1);
+      }
+
+      if (ns.size() > 0) {
+        result += ns + "::";
+      }
+    }
+
+    return result;
   }
 
  private: