<?xml version="1.0" encoding="windows-1252"?>
<node id="987593" title="Re: MooseX obscure error and importance of Unit Testing" created="2012-08-15 12:17:45" updated="2012-08-15 12:17:45">
<type id="11">
note</type>
<author id="757127">
tobyink</author>
<data>
<field name="doctext">
&lt;p&gt;The problem is, this is sloppy:&lt;/p&gt;
&lt;code&gt;
ClassName-&gt;new
&lt;/code&gt;

&lt;p&gt;It will behave differently depending on whether or not there is a sub called &lt;c&gt;ClassName&lt;/c&gt; in the current package. This is a pretty hard to debug error, and is not really Moose's fault. The following snippet illustrates the problem and doesn't use Moose anywhere...&lt;/p&gt;

&lt;code&gt;
use 5.010;
use strict;
use LWP::UserAgent;

{
  package ClassName;
  sub new { bless [@_], $_[0] }
}
{
  package main;
  sub ClassName () { 'LWP::UserAgent' };
  my $obj = ClassName-&gt;new;
  say ref $obj;  # says "LWP::UserAgent"  
}
&lt;/code&gt;

&lt;p&gt;The solution is to institute a policy of &lt;i&gt;never&lt;/i&gt; using the &lt;c&gt;BareWord-&gt;method&lt;/c&gt; syntax. Better, and unambiguous ways of writing &lt;c&gt;ClassName-&gt;new&lt;/c&gt; are:&lt;/p&gt;

&lt;code&gt;
ClassName::-&gt;new
# or...
'ClassName'-&gt;new
&lt;/code&gt;

&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; [mod://aliased] is also a rather nice solution.&lt;/p&gt;

&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-757127"&gt;
&lt;small&gt;&lt;small&gt;
&lt;tt&gt;perl -E'sub Monkey::do{say$_,for@_,do{($monkey=&amp;#x5B;caller(0)]-&gt;&amp;#x5B;3])=~s{::}{ }and$monkey}}"Monkey say"-&gt;Monkey::do'
&lt;/tt&gt;&lt;/small&gt;&lt;/small&gt;
&lt;/div&gt;&lt;/div&gt;
</field>
<field name="root_node">
987567</field>
<field name="parent_node">
987567</field>
</data>
</node>
