print "Enter the student's course(s) "; $self->{"Course(s)"}=[] #### print "Enter the student's course(s) delimiting them with a comma (,) "; chomp(my $cour = ); $cour =~ s/\s+//g; my @cour = split(",", $cour); chomp($self->{"Course(s)"}->{"cour"}=[@cour]); #### sub add_courses { my $self = shift; my $courses = shift; # $courses = ["C++", "Java"]; my $num = @{$courses}; for ($i=0; $i<$num; $i++) { push @{$self->{'Course(s)'}->{'cour'}}, $courses->[$i]; } #### Here are the statistics for the Sino. Address: kk ID: kk Major: jj Start_Date: kk Tuition: kk Course(s): Chem, Bio, Maths, C++, Java After dropping: Chem, Bio, Maths, , Java #### package Student3; sub new { my $class = shift; my $ref = {}; # Anonymous hash bless($ref, $class); return $ref; } sub set_student { my $self = shift; print "Enter the student's name "; chomp($self->{"Name"}=); print "Enter the student's major "; chomp($self->{"Major"}=); print "Enter the student's course(s) delimiting them with a comma (,) "; chomp(my $cour = ); $cour =~ s/\s+//g; my @cour = split(",", $cour); chomp($self->{"Course(s)"}->{"cour"}=[@cour]); <...continues here...> } sub show_student { my $self = shift; print "Here are the statistics for the ", $self->{"Name"}, ".\n"; foreach $key (sort(keys %$self)) { if ($self->{$key} ne $self->{"Course(s)"}) { printf "%s: %s\n", $key, $self->{$key} unless $self->{$key} eq $self->{"Name"}; } } print "Course(s): ", join (", ", @{$self->{'Course(s)'}->{'cour'}}), "\n"; } sub add_courses { my $self = shift; my $courses = shift; my $num = @{$courses}; for ($i=0; $i<$num; $i++) { push @{$self->{'Course(s)'}->{'cour'}}, $courses->[$i]; } } sub drop_courses { my $self=shift; my $del_cour = shift; # $del_cour = ["Java"]; my $num = @{$del_cour}; if ($num == 1) { foreach $k (@{$self->{'Course(s)'}->{'cour'}}) { if ($k eq $del_cour->[0]) { $k=""; } } } print "After dropping: ", join (", ", @{$self->{'Course(s)'}->{'cour'}}), "\n"; } 1; #### #!/usr/bin/perl -w use Student3; use warnings; use strict; my $student1 = Student3->new; $student1->set_student; $student1->show_student; $student1->add_courses(["C++", "Java"]); $student1->show_student; $student1->drop_courses(["C++"]);