File Coverage

blib/lib/rubyisms.pm
Criterion Covered Total %
statement 53 64 82.8
branch 11 16 68.7
condition 4 9 44.4
subroutine 14 15 93.3
pod 3 5 60.0
total 85 109 77.9


line stmt bran cond sub pod time code
1             package DB;
2 15     15 0 125 sub uplevel_args { my @foo = caller($_[0]+1); return @DB::args };
  15         56  
3              
4             package rubyisms;
5              
6 1     1   51326 use strict;
  1         3  
  1         54  
7 1     1   6 use warnings;
  1         1  
  1         35  
8 1     1   7 use Carp;
  1         7  
  1         117  
9 1     1   7 use Exporter;
  1         2  
  1         128  
10              
11             our @ISA = qw(Exporter);
12             our @EXPORT = qw( self super yield );
13             our $VERSION = '1.0';
14              
15             sub import {
16 1     1   6 no strict 'refs';
  1         2  
  1         1731  
17 4 100   4   9629 push @{(caller())[0]."::ISA"}, "Class"
  3         107  
18             unless (caller())[0] eq "Class"; # Although Perl should deal it anyway
19 4         8495 rubyisms->export_to_level(1, @_);
20             }
21              
22             sub self () {
23 8     8 1 410 my $call_pack = (caller())[0];
24              
25             # So we're looking for the first thing that ISA $call_pack
26 8         15 my $level =1;
27 8         33 while (caller($level)) {
28 11         564 my @their_args = DB::uplevel_args($level);
29 11 100 66     40 if (ref $their_args[0]
30 8         151 and eval { $their_args[0]->isa($call_pack) }) {
31 8         50 return $their_args[0];
32             }
33 3         9 $level++;
34             }
35             # Well, hey, maybe it's a class method.
36 0         0 return $call_pack;
37             }
38              
39             sub yield (@) {
40 4     4 1 37 my @their_args = DB::uplevel_args(1);
41 4 50 33     31 if ((!@their_args) or ref $their_args[0] ne "CODE") {
42 0         0 croak "no block given (LocalJumpError)";
43             }
44 4   33     14 my @stuff = (@_||$_);
45 4 100       532 $their_args[0]->(@stuff)
46             unless $stuff[0] == $their_args[0]; #Don't yield onto yourself.
47             }
48              
49              
50             sub super() {
51 3 50   3 1 709 if (@_) {
52             # Someone's trying to find SUPER's super. Blah.
53 3         11 goto &UNIVERSAL::super;
54             }
55 0         0 @_ = DB::uplevel_args(1);
56 0         0 my $self = $_[0];
57 0 0       0 if (!$self) { carp "super called outside of method" }
  0         0  
58 0         0 my $caller= (caller(1))[3];
59 0         0 $caller =~ s/.*:://;
60 0         0 goto &{$self->UNIVERSAL::super($caller)};
  0         0  
61             }
62              
63             package Class;
64             rubyisms->import;
65              
66             sub new {
67 1     1   1846 my $class = shift;
68 1         83 my $self = bless {}, $class;
69             # Cheat.
70 1         4 my @args = @_;
71 1         4 @_ = $self;
72 1     1   8 no strict 'refs';
  1         2  
  1         290  
73 1         6 $self->initialize(@args);
74 1         624 return self();
75             }
76              
77 0     0   0 sub initialize { } # Just to be sure.
78              
79             package UNIVERSAL;
80              
81             sub super {
82 4     4 0 10 my ($class, $method) = @_;
83              
84 4 100       14 if (ref $class) { $class = ref $class; }
  2         4  
85 4         7 my $x;
86 1     1   5 no strict 'refs';
  1         2  
  1         190  
87 4         6 for (@{$class."::ISA"}, "UNIVERSAL") {
  4         24  
88 4 50       62 return $x if $x = $_->can($method);
89             }
90             }
91              
92             # Preloaded methods go here.
93              
94             1;
95             __END__