|
|
| Pathologically Eclectic Rubbish Lister | |
| PerlMonks |
Re^5: MooseX obscure error and importance of Unit Testing (tools)by tobyink (Monsignor) |
| on Aug 16, 2012 at 07:11 UTC ( #987702=note: print w/ replies, xml ) | Need Help?? |
|
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.
Most of the penalty appears to be at compile-time, but the following illustrates that there's a small performance penalty at runtime too:
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'
In Section
Meditations
|
|
||||||||||||||||||||||