title: In Perl, is there any harm in creating a subroutine with the same name as a package?
body: <p>Say I have a package called <code>My::Pkg</code>, and that package has a <code>-&gt;new(...)</code> class method to instantiate new objects:</p>
  
  <pre><code>package My::Pkg;
  
  sub new {bless {@_[1..$#_]} =&gt; $_[0]}
  </code></pre>
  
  <p>Is there any harm in defining the following subroutine:</p>
  
  <pre><code>sub My::Pkg {@_ ? My::Pkg::new('My::Pkg', @_) : 'My::Pkg'}
  </code></pre>
  
  <p>So that someone could write:</p>
  
  <pre><code>my $obj = My::Pkg one =&gt; 1, two =&gt; 2;
  </code></pre>
  
  <p>Rather than:</p>
  
  <pre><code>my $obj = My::Pkg-&gt;new(one =&gt; 1, two =&gt; 2); # which still works, but is longer
  </code></pre>
  
  <p>I like the terseness of the package-named-constructor-subroutine method, but I am interested to know if there are any hidden gotchas to this technique that I have not thought of.</p>
  
  <hr>
  
  <p>Update:</p>
  
  <p>Inheritance works correctly, as shown by the example here:</p>
  
  <pre><code>{package a; sub new {say "a::new [@_] ", $_[0]-&gt;init}}
  {package b;    our @ISA = 'a'; sub init {"(b::init [@_])"}}
  {package a::b; our @ISA = 'b';}
  
  sub a::b {print "absub [@_], "; 'a::b'}
  
  # a::b() called with no args, returns 'a::b', which then becomes 'a::b'-&gt;new(...)
  a::b-&gt;new;            # absub [], a::new [a::b] (b::init [a::b])
  a::b-&gt;new(1, 2, 3);   # absub [], a::new [a::b 1 2 3] (b::init [a::b])    
  
  # no call to `a::b()` but otherwise the same:
  'a::b'-&gt;new;          # a::new [a::b] (b::init [a::b])
  'a::b'-&gt;new(1, 2, 3); # a::new [a::b 1 2 3] (b::init [a::b])
  
  new a::b::;           # a::new [a::b] (b::init [a::b])
  new a::b:: 1, 2, 3;   # a::new [a::b 1 2 3] (b::init [a::b])
  </code></pre>
  
  <p>Interestingly the only thing so far that is different is that the following 2 lines become syntax errors:</p>
  
  <pre><code>new a::b;
  new a::b 1, 2, 3;
  </code></pre>
  
  <p>Which is a syntax error for the same reason <code>some_undefined_sub some_defined_sub;</code> is one.</p>
  
  <p>If the <code>new</code> subroutine is defined, it is parsed as <code>new( a::b(...) )</code> which is normal for two adjacent bareword subroutines.</p>
  
  <p>Personally, I am ok with <code>new a::b</code> becoming a syntax error, the unambiguous version <code>new a::b::</code> will always work as <em>tchrist</em> helpfully points out below.</p>