Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
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 scrutinizing the Monastery: (17)
As of 2014-12-18 15:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (58 votes), past polls