Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: MooseX obscure error and importance of Unit Testing

by Anonymous Monk
on Aug 16, 2012 at 03:23 UTC ( #987682=note: print w/ replies, xml ) Need Help??


in reply to Re: MooseX obscure error and importance of Unit Testing
in thread MooseX obscure error and importance of Unit Testing

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

LOL


Comment on Re^2: MooseX obscure error and importance of Unit Testing
Re^3: MooseX obscure error and importance of Unit Testing
by chromatic (Archbishop) on Aug 16, 2012 at 05:12 UTC

    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        

        If you don't like silent failures, then you should be a big fan of ClassName::->new. With warnings enabled it will emit a compile-time warning if the ClassName package hasn't been loaded.

        Update: also according to my benchmarking (under Perl 5.16.0), ClassName::->new runs faster than ClassName->new. 'ClassName'->new is somewhere in between; ClassName->new is the slowest.

        use Benchmark; sub new { bless \@_, $_[0] } timethese(100_000, { bareword => sub { eval q{ main->new } }, quoted => sub { eval q{ 'main'->new } }, colons => sub { eval q{ main::->new } }, });

        Most of the penalty appears to be at compile-time, but the following illustrates that there's a small performance penalty at runtime too:

        use Benchmark; sub new { bless \@_, $_[0] } timethese(1_000_000, { bareword => sub { main->new }, quoted => sub { 'main'->new }, colons => sub { main::->new }, });

        So given that ClassName::->new is unambiguous, faster and can provide helpful warning messages when lexical warnings are enabled, what possible argument (other than force of habit) can there be for using ClassName->new? In cases where the warnings need to be suppressed (e.g. calling a class method on a class loaded at run-time), then 'ClassName'->new can be used, because that is also unambiguous, and also faster than ClassName->new.

        Update 2: further testing with the runtime-only tests seems to show that the quoted version may actually be fastest at run time. The bareword version is still slowest though. The version with the colons wins hands-down at compile-time. Either way, the speed difference is minor - the primary argument against the bareword version is its ambiguity.

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

        What makes you feel Moose pushes strongly toward bad class design?

        Three thousand years of beautiful tradition, from Moses to Sandy Koufax, you're god damn right I'm living in the fucking past

      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://987682]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (12)
As of 2014-09-23 13:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (222 votes), past polls