Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: MooseX obscure error and importance of Unit Testing

by tobyink (Abbot)
on Aug 15, 2012 at 16:17 UTC ( #987593=note: print w/ replies, xml ) Need Help??


in reply to MooseX obscure error and importance of Unit Testing

The problem is, this is sloppy:

ClassName->new

It will behave differently depending on whether or not there is a sub called ClassName 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...

use 5.010; use strict; use LWP::UserAgent; { package ClassName; sub new { bless [@_], $_[0] } } { package main; sub ClassName () { 'LWP::UserAgent' }; my $obj = ClassName->new; say ref $obj; # says "LWP::UserAgent" }

The solution is to institute a policy of never using the BareWord->method syntax. Better, and unambiguous ways of writing ClassName->new are:

ClassName::->new # or... 'ClassName'->new

Update: aliased is also a rather nice solution.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'


Comment on Re: MooseX obscure error and importance of Unit Testing
Select or Download Code
Re^2: MooseX obscure error and importance of Unit Testing
by Anonymous Monk on Aug 16, 2012 at 03:23 UTC

    The solution is to institute a policy of never using the BareWord->method syntax

    LOL

      Do you know a more reliable solution, other than constructing optrees by hand?

        I prefer: 1) Don't use tools that make subroutines having the same names as classes. 2) Don't use tools (like old base.pm) that silently ignore failures when you try to require a module.

        But since I find Moose so strongly pushes toward bad class design (and MooseX::Declare leads to obscure errors and days wasted debugging), not using them is no hardship for me anyway. :)

        - tye        

        Do you know a more reliable solution, other than constructing optrees by hand?

        I don't need one, I manage to avoid writing subroutines that clash with classes, pretty much the same way most of CPAN and everyone else seems to, fairly easily :)

        Although this might be a candidate :) subs::auto - Read barewords as subroutine names.

        This pragma lexically enables the parsing of any bareword as a subroutine name, except those which corresponds to an entry in %INC (expected to be class names) or whose symbol table entry has an IO slot (expected to be filehandles).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://987593]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2015-07-06 01:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (68 votes), past polls