While digging around in the debugger, I came across a method on a an object that I wasn't expecting to see. So I ack'd for it.
$ ack -a 'a_sub_not_likely_to_be_here' deps deps/lib/perl5/Set/Scalar/Base.pm 20: eval 'sub UNIVERSAL::a_sub_not_likely_to_be_here { ref($_[0]) }'; 24: ? eval { $_[0]->a_sub_not_likely_to_be_here } deps/lib/perl5/JSON/PP.pm 1285: eval 'sub UNIVERSAL::a_sub_not_likely_to_be_here { ref($_[0]) }'; 1288: ref($_[0]) ? eval { $_[0]->a_sub_not_likely_to_be_here } : undef; deps/lib/perl5/Mouse/Util.pm 14: *UNIVERSAL::a_sub_not_likely_to_be_here = sub { 30: ? eval { $_[0]->a_sub_not_likely_to_be_here } 53: eval { $r->a_sub_not_likely_to_be_here; 1 } deps/lib/perl5/Mouse/Tiny.pm 32: *UNIVERSAL::a_sub_not_likely_to_be_here = sub { 48: ? eval { $_[0]->a_sub_not_likely_to_be_here } 71: eval { $r->a_sub_not_likely_to_be_here; 1 }
Why do four different modules do this?
Mouse (and thus Mouse::Tiny, which is a one-file concatenation of the Mouse libraries) use this because Scalar::Util's pure-perl version does, to emulate Scalar::Util::blessed without XS.
More recent versions of Mouse have removed this bit of code, deciding that (at least for now), if you want 5.6 you get a dependency on Scalar::Util.
I've got a bloody good mind to write Break::Mouse which will delete that subroutine if it ever spots that it's been created. And load it into my CPAN-testing machines.
This shows why monkey-patching is generally a bad idea.
package Break::Mouse;
use Time::HiRes qw(setitimer ITIMER_VIRTUAL);
setitimer(ITIMER_VIRTUAL, 1, 1);
$SIG{VTALRM} = sub {
eval 'sub UNIVERSAL::a_sub_not_likely_to_be_here { die("Monkey-patching is bad\n")';
}
Re:I'm being evil
sartak on 2009-01-09T12:31:52
Evil indeed.
So how do we implement blessed without XS? As I said in a sibling comment, Mouse doesn't do this any more, it now just depends on Scalar::Util. But since I just took the code from Scalar::Util, so your package should be named Break::Scalar::Util.
Re:I'm being evil
drhyde on 2009-01-09T13:00:16
Check to see if it's a reference and *isn't* a reference to a scalar/array/hash/sub etc. The list of magic values returned by
ref()
for all the built-in types is inperldoc -f ref
.Re:I'm being evil
Stevan on 2009-01-09T20:01:07
Seems silly to reinvent the wheel when Scalar::Util already has a nice shiny round one in the core.Re:I'm being evil
Aristotle on 2009-01-11T22:09:38
UNIVERSAL::can( $foo, 'can' )
Re:I'm being evil
sartak on 2009-01-11T23:17:03
That's.. remarkably sane. You should submit a patch to Graham.Re:I'm being evil
Aristotle on 2009-01-11T23:31:01
Repo URL (Github preferred)? Email address?
Re:I'm being evil
sartak on 2009-01-11T23:42:45
I stole the code from Scalar::Util (hence why the patch should go to Graham); it no longer lives in Mouse because I now just have a dependency on it. Scalar::Util lives in core as of 5.8, but it'd be a useful fix for 5.6.Re:I'm being evil
Aristotle on 2009-01-12T00:11:08
Ah.
Re:I'm being evil
Stevan on 2009-01-09T20:31:35
Wow, thats a loooong way to go to prove a point! (especially since the point it no longer valid (see Sartak's response below)).
Not to mention the fact that it is a generally bad idea since it will break Scalar::Util (and Mouse too) which is a core module, therefore resulting in a whole lot of false negatives from your CPAN testing machines.
While I normally agree with you that monkeypatching is an evil practice, like all evil practices there are some exceptions. In this case what Sartak was doing in Mouse was for the greater good (making it possible to use the Moose/Mouse goodness in a zero-dependency fashion) and done with full knowledge of the rules he was breaking and lines he was crossing.
Please don't just jump to conclusions about something like this, and do some research first before you threaten to f*ck up a bunch of CPAN-testers results for no good reason.
Re:I'm being evil
drhyde on 2009-01-09T20:42:31
Please return your humour-detector to the shop for a repacement. It is faulty.Re:I'm being evil
Stevan on 2009-01-09T21:51:11
I don't find threats funny,
... and neither does the Dept. of Homeland Security!!! YOU ARE NOW ON THE WATCH LIST!!!!!!!!
:P Re:I'm being evil
chromatic on 2009-01-10T02:30:14
In this case what Sartak was doing in Mouse was for the greater good (making it possible to use the Moose/Mouse goodness in a zero-dependency fashion)....
Alternately, you could target only sane and modern versions of Perl, in the theory that anyone knowledgeable enough to use Mouse or Moose should be able to rely on a Perl version released this millennium.