New.pm untested

filter: Statementstmt 0.0% 0 / 64 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 93 CC 4 SCAR 15.9
CC
4
Cov
0%
write_blessing
12.0
write_attributes
6.0
new
2.0
CRAP 4.9
1 package Class::Classgen::New;
2  
3 $VERSION=3.03;
4  
5  
6 # 3.03:
7 # No changes in here 02.10.2000
8  
9 # 3.02 Thu Jun 06 2000
10 # - according to lieting@mailconcept.com using a variable $type
11 # in the control file gets confused with the internal variable
12 # my $type = ref($self)||$self; in the constructor new().
13 # This problem has been corrected.
14  
15 use strict;
16 use Class::Classgen::Comments; # to remove problems by typing errors
17  
18 sub new { # constructor
19 my ($self, $ref) = @_;
20 my $type = ref($self)||$self;
21  
22 # instance-variables
23 my $_rAttr;
24 my $_type;
25
26 $self=bless {
27 _ref => $_rAttr,
28 _type => $_type
29 }, $type;
30
31 $self->set_ref($ref);
32 return $self;
33 }
34  
35 sub get_ref {
36 my ($self) = @_;
37 $self->{_ref};
38 }
39  
40 sub get_type {
41 my ($self) = @_;
42 $self->{_type};
43 }
44  
45 sub set_ref {
46 my ($self, $ref) = @_;
47 $self->{_ref} = $ref;
48 }
49  
50 sub set_type {
51 my ($self, $type) = @_;
52 $self->{_type} = $type;
53 }
54  
55 sub write_attributes {
56 my ($self) = @_;
57 my $rl = $self->get_ref();
58 my $attr;
59 my $s;
60 my $var;
61 my $com;
62  
63 foreach $attr (@$rl) {
64 # $s.= "\tmy ". $attr->get_var() .";\n";
65 next unless( $attr->get_var()=~m/[\$\%\@]/ ); # allowed types
66 $var = Class::Classgen::Comments::just_var( $attr->get_var() );
67 $com = Class::Classgen::Comments::just_comments( $attr->get_var() );
68 $s.= "\tmy ". $var .";\t$com\n";
69 }
70 return $s;
71 }
72  
73 sub write_blessing {
74 my ($self) = @_;
75 my $ral = $self->get_ref();
76 my $s;
77
78 if( $self->get_type() =~ m/{/ ) {
79 my $attr;
80 foreach $attr (@$ral) {
81 $s.=$attr->generate_blessed_h();
82 }
83 }
84
85 if( $self->get_type() =~ m/\[/ ) {
86 print "blessing of anonymous array [] not implemented yet\n";
87 }
88 return $s;
89 }
90  
91 sub write_code {
92 my ($self) = @_;
93 my $s='';
94 my $t='';
95 my $u='';
96 my $v='';
97 my $w='';
98
99 $s = "sub new {\n";
100 $s.= "\tmy (\$self) = \@_;\n";
101 # $s.= "\tmy \$type = ref(\$self)||\$self;\n"; # 3.01 -> 3.02
102  
103 $s.= "\n\t\# instance-variables:\n";
104  
105 $t = $self->write_attributes();
106  
107 $u = "\n";
108 $u.= "\t\$self=bless {\n";
109
110 $v = $self->write_blessing();
111
112 # $w = "\t}, \$type;\n"; # 3.01
113 $w = "\t}, ref(\$self)||\$self;\n"; # 3.01 -> 3.02
114  
115 # to put in some default code which supports inheritance
116 $w.= "\t\#\$self->inherit_from(\$self->your_base::new());";
117 $w.= "\t\# adapt when inheriting\n";
118  
119 $w.= "\treturn \$self;\n";
120 $w.= "}\n\n";
121
122 return $s.$t.$u.$v.$w;
123 }
124
125 1;
126  
127 __END__