moose-test.pl 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env perl
  2. # proof that moose types suck, they do only checks but no optimizations.
  3. # see also #350
  4. #
  5. # with an older B::C < ~1.40 out of memory while compiling.
  6. # endless recursion with B::HV::save
  7. # 7 0x521293e4 in Perl_pp_entersub () at pp_hot.c:2806
  8. # GVGV::GV = 0x14c3ad0 "B::HV" :: "save", DEPTH = 44817
  9. {
  10. package Foo::Moose;
  11. use Moose;
  12. has bar => (is => 'rw');
  13. __PACKAGE__->meta->make_immutable;
  14. }
  15. {
  16. package TypedFoo::Moose;
  17. use Moose;
  18. has bar => (is => 'rw', isa => 'Int');
  19. __PACKAGE__->meta->make_immutable;
  20. }
  21. {
  22. package Foo::Manual;
  23. sub new { bless {} => shift }
  24. sub bar {
  25. my $self = shift;
  26. return $self->{bar} unless @_;
  27. $self->{bar} = shift;
  28. }
  29. }
  30. my $foo1 = Foo::Moose->new;
  31. sub moose {
  32. $foo1->bar(32);
  33. my $x = $foo1->bar;
  34. }
  35. my $foo2 = TypedFoo::Moose->new;
  36. sub moosetyped {
  37. $foo2->bar(32);
  38. my $x = $foo2->bar;
  39. }
  40. my $foo = Foo::Manual->new;
  41. sub manual {
  42. $foo->bar(32);
  43. my $x = $foo->bar;
  44. }
  45. use Benchmark 'timethese';
  46. print "Testing Perl $]\n";
  47. timethese(
  48. 50_000,
  49. {
  50. moose => \&moose,
  51. moosetyped => \&moosetyped,
  52. manual => \&manual,
  53. }
  54. );