line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::Random; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# $Id: Random.pm,v 1.3 2002/08/06 17:02:37 pmh Exp $ |
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
4775
|
use overload '""','stringify'; |
|
2
|
|
|
|
|
2434
|
|
|
2
|
|
|
|
|
13
|
|
6
|
2
|
|
|
2
|
|
118
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
63
|
|
7
|
2
|
|
|
2
|
|
10
|
use Carp; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
189
|
|
8
|
2
|
|
|
2
|
|
12
|
use vars qw($VERSION); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
661
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
$VERSION=0.2; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub stringify{ |
13
|
6
|
|
|
6
|
0
|
10
|
my($self)=@_; |
14
|
|
|
|
|
|
|
|
15
|
6
|
100
|
|
|
|
18
|
if($self->{mode} eq 'choose'){ |
|
|
50
|
|
|
|
|
|
16
|
3
|
|
|
|
|
4
|
@{$self->{isa}}=($self,@{$self->{list}[rand @{$self->{list}}]}); |
|
3
|
|
|
|
|
66
|
|
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
54
|
|
17
|
|
|
|
|
|
|
}elsif($self->{mode} eq 'shuffle'){ |
18
|
3
|
|
|
|
|
4
|
my @list=@{$self->{list}}; |
|
3
|
|
|
|
|
7
|
|
19
|
3
|
|
|
|
|
4
|
my @isa; |
20
|
3
|
|
|
|
|
17
|
while(@list){ |
21
|
6
|
|
|
|
|
22
|
push @isa,splice @list,rand @list,1; |
22
|
|
|
|
|
|
|
} |
23
|
3
|
|
|
|
|
4
|
@{$self->{isa}}=($self,@isa); |
|
3
|
|
|
|
|
27
|
|
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
# Return a class with no methods, so the method search doesn't stop here |
26
|
6
|
|
|
|
|
1899
|
'Class::Random::Empty'; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub import{ |
30
|
3
|
|
|
3
|
|
21
|
my($pack,$mode,@list)=@_; |
31
|
3
|
50
|
|
|
|
12
|
@_>3 |
32
|
|
|
|
|
|
|
or croak "Usage: use $pack BEHAVIOUR => LIST;"; |
33
|
3
|
|
|
|
|
7
|
my $callpkg=caller; |
34
|
|
|
|
|
|
|
|
35
|
3
|
100
|
100
|
|
|
20
|
if($mode eq 'choose' || $mode eq 'shuffle'){ |
|
|
50
|
|
|
|
|
|
36
|
2
|
100
|
|
|
|
5
|
if($mode eq 'choose'){ |
37
|
1
|
|
|
|
|
3
|
foreach(@list){ |
38
|
2
|
50
|
|
|
|
7
|
ref eq 'ARRAY' |
39
|
|
|
|
|
|
|
or croak "choose argument must be list of lists"; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
my $self=bless { |
44
|
|
|
|
|
|
|
mode => $mode, |
45
|
|
|
|
|
|
|
list => \@list, |
46
|
2
|
|
|
|
|
3
|
isa => do{ |
47
|
2
|
|
|
2
|
|
10
|
no strict 'refs'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
239
|
|
48
|
2
|
|
|
|
|
2
|
\@{$callpkg.'::ISA'}; |
|
2
|
|
|
|
|
14
|
|
49
|
|
|
|
|
|
|
}, |
50
|
|
|
|
|
|
|
}; |
51
|
2
|
|
|
|
|
4
|
unshift @{$self->{isa}},$self; |
|
2
|
|
|
|
|
49
|
|
52
|
2
|
|
|
|
|
12
|
return "$self"; # Force setting of caller's @ISA |
53
|
|
|
|
|
|
|
}elsif($mode eq 'subclass'){ |
54
|
|
|
|
|
|
|
my $new=sub{ |
55
|
100
|
|
|
100
|
|
604
|
my $pkg=shift; |
56
|
100
|
|
|
|
|
304
|
$list[rand @list]->new(@_); |
57
|
1
|
|
|
|
|
8
|
}; |
58
|
2
|
|
|
2
|
|
8
|
no strict 'refs'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
240
|
|
59
|
1
|
|
|
|
|
1
|
*{$callpkg.'::new'}=$new; |
|
1
|
|
|
|
|
1576
|
|
60
|
|
|
|
|
|
|
}else{ |
61
|
0
|
|
|
|
|
|
croak "Unknown $pack mode: $mode\n"; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
package Class::Random::Empty; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
1; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
__END__ |