Filename | /usr/share/perl/5.18/base.pm |
Statements | Executed 91 statements in 1.31ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
3 | 3 | 3 | 237µs | 267µs | import | base::
1 | 1 | 1 | 15µs | 30µs | BEGIN@3 | base::
3 | 1 | 1 | 13µs | 13µs | has_fields | base::
3 | 1 | 1 | 9µs | 9µs | has_attr | base::
1 | 1 | 1 | 9µs | 40µs | BEGIN@4 | base::
0 | 0 | 0 | 0s | 0s | __ANON__[:47] | base::
0 | 0 | 0 | 0s | 0s | __ANON__[:54] | base::
0 | 0 | 0 | 0s | 0s | get_attr | base::
0 | 0 | 0 | 0s | 0s | inherit_fields | base::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package base; | ||||
2 | |||||
3 | 2 | 34µs | 2 | 46µs | # spent 30µs (15+16) within base::BEGIN@3 which was called:
# once (15µs+16µs) by Variable::Magic::BEGIN@688 at line 3 # spent 30µs making 1 call to base::BEGIN@3
# spent 16µs making 1 call to strict::import |
4 | 2 | 982µs | 2 | 72µs | # spent 40µs (9+31) within base::BEGIN@4 which was called:
# once (9µs+31µs) by Variable::Magic::BEGIN@688 at line 4 # spent 40µs making 1 call to base::BEGIN@4
# spent 31µs making 1 call to vars::import |
5 | 1 | 1µs | $VERSION = '2.18'; | ||
6 | 1 | 18µs | $VERSION = eval $VERSION; # spent 3µs executing statements in string eval | ||
7 | |||||
8 | # constant.pm is slow | ||||
9 | sub SUCCESS () { 1 } | ||||
10 | |||||
11 | sub PUBLIC () { 2**0 } | ||||
12 | sub PRIVATE () { 2**1 } | ||||
13 | sub INHERITED () { 2**2 } | ||||
14 | sub PROTECTED () { 2**3 } | ||||
15 | |||||
16 | 1 | 1µs | my $Fattr = \%fields::attr; | ||
17 | |||||
18 | # spent 13µs within base::has_fields which was called 3 times, avg 4µs/call:
# 3 times (13µs+0s) by base::import at line 100, avg 4µs/call | ||||
19 | 3 | 3µs | my($base) = shift; | ||
20 | 3 | 3µs | my $fglob = ${"$base\::"}{FIELDS}; | ||
21 | 3 | 15µs | return( ($fglob && 'GLOB' eq ref($fglob) && *$fglob{HASH}) ? 1 : 0 ); | ||
22 | } | ||||
23 | |||||
24 | # spent 9µs within base::has_attr which was called 3 times, avg 3µs/call:
# 3 times (9µs+0s) by base::import at line 100, avg 3µs/call | ||||
25 | 3 | 2µs | my($proto) = shift; | ||
26 | 3 | 2µs | my($class) = ref $proto || $proto; | ||
27 | 3 | 11µs | return exists $Fattr->{$class}; | ||
28 | } | ||||
29 | |||||
30 | sub get_attr { | ||||
31 | $Fattr->{$_[0]} = [1] unless $Fattr->{$_[0]}; | ||||
32 | return $Fattr->{$_[0]}; | ||||
33 | } | ||||
34 | |||||
35 | 1 | 1µs | if ($] < 5.009) { | ||
36 | *get_fields = sub { | ||||
37 | # Shut up a possible typo warning. | ||||
38 | () = \%{$_[0].'::FIELDS'}; | ||||
39 | my $f = \%{$_[0].'::FIELDS'}; | ||||
40 | |||||
41 | # should be centralized in fields? perhaps | ||||
42 | # fields::mk_FIELDS_be_OK. Peh. As long as %{ $package . '::FIELDS' } | ||||
43 | # is used here anyway, it doesn't matter. | ||||
44 | bless $f, 'pseudohash' if (ref($f) ne 'pseudohash'); | ||||
45 | |||||
46 | return $f; | ||||
47 | } | ||||
48 | } | ||||
49 | else { | ||||
50 | *get_fields = sub { | ||||
51 | # Shut up a possible typo warning. | ||||
52 | () = \%{$_[0].'::FIELDS'}; | ||||
53 | return \%{$_[0].'::FIELDS'}; | ||||
54 | } | ||||
55 | 1 | 4µs | } | ||
56 | |||||
57 | # spent 267µs (237+30) within base::import which was called 3 times, avg 89µs/call:
# once (84µs+11µs) by Class::Method::Modifiers::BEGIN@14 at line 14 of Class/Method/Modifiers.pm
# once (77µs+10µs) by Variable::Magic::BEGIN@688 at line 688 of Variable/Magic.pm
# once (76µs+9µs) by namespace::clean::_Util::BEGIN@15 at line 15 of namespace/clean/_Util.pm | ||||
58 | 3 | 2µs | my $class = shift; | ||
59 | |||||
60 | 3 | 2µs | return SUCCESS unless @_; | ||
61 | |||||
62 | # List of base classes from which we will inherit %FIELDS. | ||||
63 | 3 | 600ns | my $fields_base; | ||
64 | |||||
65 | 3 | 3µs | my $inheritor = caller(0); | ||
66 | |||||
67 | 3 | 1µs | my @bases; | ||
68 | 3 | 4µs | foreach my $base (@_) { | ||
69 | 3 | 2µs | if ( $inheritor eq $base ) { | ||
70 | warn "Class '$inheritor' tried to inherit from itself\n"; | ||||
71 | } | ||||
72 | |||||
73 | 3 | 51µs | 3 | 7µs | next if grep $_->isa($base), ($inheritor, @bases); # spent 7µs making 3 calls to UNIVERSAL::isa, avg 2µs/call |
74 | |||||
75 | # Following blocks help isolate $SIG{__DIE__} changes | ||||
76 | { | ||||
77 | 6 | 2µs | my $sigdie; | ||
78 | { | ||||
79 | 6 | 8µs | local $SIG{__DIE__}; | ||
80 | 3 | 71µs | eval "require $base"; # spent 8µs executing statements in 3 string evals (merged) | ||
81 | # Only ignore "Can't locate" errors from our eval require. | ||||
82 | # Other fatal errors (syntax etc) must be reported. | ||||
83 | 3 | 900ns | die if $@ && $@ !~ /^Can't locate .*? at \(eval /; | ||
84 | 3 | 8µs | unless (%{"$base\::"}) { | ||
85 | require Carp; | ||||
86 | local $" = " "; | ||||
87 | Carp::croak(<<ERROR); | ||||
88 | Base class package "$base" is empty. | ||||
89 | (Perhaps you need to 'use' the module which defines that package first, | ||||
90 | or make that module available in \@INC (\@INC contains: @INC). | ||||
91 | ERROR | ||||
92 | } | ||||
93 | 3 | 12µs | $sigdie = $SIG{__DIE__} || undef; | ||
94 | } | ||||
95 | # Make sure a global $SIG{__DIE__} makes it out of the localization. | ||||
96 | 3 | 2µs | $SIG{__DIE__} = $sigdie if defined $sigdie; | ||
97 | } | ||||
98 | 3 | 2µs | push @bases, $base; | ||
99 | |||||
100 | 3 | 14µs | 6 | 23µs | if ( has_fields($base) || has_attr($base) ) { # spent 13µs making 3 calls to base::has_fields, avg 4µs/call
# spent 9µs making 3 calls to base::has_attr, avg 3µs/call |
101 | # No multiple fields inheritance *suck* | ||||
102 | if ($fields_base) { | ||||
103 | require Carp; | ||||
104 | Carp::croak("Can't multiply inherit fields"); | ||||
105 | } else { | ||||
106 | $fields_base = $base; | ||||
107 | } | ||||
108 | } | ||||
109 | } | ||||
110 | # Save this until the end so it's all or nothing if the above loop croaks. | ||||
111 | 3 | 32µs | push @{"$inheritor\::ISA"}, @bases; | ||
112 | |||||
113 | 3 | 14µs | if( defined $fields_base ) { | ||
114 | inherit_fields($inheritor, $fields_base); | ||||
115 | } | ||||
116 | } | ||||
117 | |||||
118 | sub inherit_fields { | ||||
119 | my($derived, $base) = @_; | ||||
120 | |||||
121 | return SUCCESS unless $base; | ||||
122 | |||||
123 | my $battr = get_attr($base); | ||||
124 | my $dattr = get_attr($derived); | ||||
125 | my $dfields = get_fields($derived); | ||||
126 | my $bfields = get_fields($base); | ||||
127 | |||||
128 | $dattr->[0] = @$battr; | ||||
129 | |||||
130 | if( keys %$dfields ) { | ||||
131 | warn <<"END"; | ||||
132 | $derived is inheriting from $base but already has its own fields! | ||||
133 | This will cause problems. Be sure you use base BEFORE declaring fields. | ||||
134 | END | ||||
135 | |||||
136 | } | ||||
137 | |||||
138 | # Iterate through the base's fields adding all the non-private | ||||
139 | # ones to the derived class. Hang on to the original attribute | ||||
140 | # (Public, Private, etc...) and add Inherited. | ||||
141 | # This is all too complicated to do efficiently with add_fields(). | ||||
142 | while (my($k,$v) = each %$bfields) { | ||||
143 | my $fno; | ||||
144 | if ($fno = $dfields->{$k} and $fno != $v) { | ||||
145 | require Carp; | ||||
146 | Carp::croak ("Inherited fields can't override existing fields"); | ||||
147 | } | ||||
148 | |||||
149 | if( $battr->[$v] & PRIVATE ) { | ||||
150 | $dattr->[$v] = PRIVATE | INHERITED; | ||||
151 | } | ||||
152 | else { | ||||
153 | $dattr->[$v] = INHERITED | $battr->[$v]; | ||||
154 | $dfields->{$k} = $v; | ||||
155 | } | ||||
156 | } | ||||
157 | |||||
158 | foreach my $idx (1..$#{$battr}) { | ||||
159 | next if defined $dattr->[$idx]; | ||||
160 | $dattr->[$idx] = $battr->[$idx] & INHERITED; | ||||
161 | } | ||||
162 | } | ||||
163 | |||||
164 | 1 | 5µs | 1; | ||
165 | |||||
166 | __END__ |