Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^5: MooseX obscure error and importance of Unit Testing (tools)

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

in reply to Re^4: MooseX obscure error and importance of Unit Testing (tools)
in thread MooseX obscure error and importance of Unit Testing

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'

Replies are listed 'Best First'.
Re^6: MooseX obscure error and importance of Unit Testing (new tools)
by tye (Sage) on Aug 16, 2012 at 07:49 UTC

    When I first saw ClassName::->new() proposed as a way to prevent ambiguous interpretation of ClassName->new(), it was shortly revealed that it didn't actually result in unambiguous parsing. It sounds like Perl's parsing may have been changed to improve that.

    I'd actually rather more improvements be done so ClassName->new() is unambiguous [or at least generates a warning when it isn't unambiguously 'ClassName'->new()]. I'd much prefer to have to actually write (the first "()" in) Function()->new() when that is what I want. That turned out to be the sane thing for both $hash{BareWord} and BareWord => $value, despite it taking years to realize that.

    For now, I'm not just yet going to run off and adopt the new fad considering the history of the many things in Perl that I've been told I should just stop using the old alternatives to, only to later find the new "must use" feature getting seriously down-graded, often out-right deprecated: v strings, pseudo hashes,,,, LVALUE subs, inside-out objects, source filters (actually, that last one became unpopular almost as soon as it became possible).

    Then there are the things that I've already realized serious problems with that the broader community may eventually clue in to: using inheritance for things like Exporter / AutoLoader / DynaLoader, 'use warnings' in modules or in test suites, given/when, 'smart' match (it smarts!), { ... redo ... }, inheritance, my $x = shift, objects as glorified hashes, Moose, MooseX::Declare (wait, I already said "source filters"). I'm sure I'm leaving a few out. :)

    But I'll keep it in mind. I appreciate the reminder of this potential pit-fall and the note about the compile-time warning.

    - tye        

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://987702]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2017-02-23 21:29 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (351 votes). Check out past polls.