A:50:r1
B:45:r2
C:40:r3
####
##########################################
# FINDING DETAILS OF A STUDENT: #####
##########################################
#!/usr/bin/perl
use warnings;
use strict;
########################
# DATABASE Input: ###
########################
print "\n\n Please ENTER Database Name (.txt): ";
my $DNAfilename=;
chomp $DNAfilename;
# open the file, or exit:
unless (open(DNAFILE, $DNAfilename) ) {
print "Cannot open file \"$DNAfilename\"\n\n";
exit;}
my @DNA= ;
print"\n $DNAfilename Database:\n\n";
print @DNA;
print"\n";
##########################
## CODE STARTS HERE:
##########################
{package Student;
sub new
{my ($name, $age, $regd_no)=@_;
my $self={ NAME => $name, AGE => $age,
REGD_NO => $regd_no};
return bless $self, my $class;print"\n class: $class\n"; }
sub display
{ my ($self) = @_;
if ($self)
{ print 'Student Name: ', $self->{NAME}, "\n";
print 'Age (yr): ', $self->{AGE}, "\n";
print 'Regd no: ', $self->{REGD_NO}, "\n";
}
}
}
my @students;
while () {chomp; push @students, Student->new(split /:/);}
print"\n WELCOME to Students' Database:\n";
print"\n Enter the Registration Number to view the details: ";
my $reg=; chomp $reg;
find_student($reg)->display(); # LINE 45
sub find_student
{my ($regd_no) = @_;
print"\n\n";
print"\n The details of the student are:\n\n";
for (@students) {return $_ if $_->{REGD_NO} eq $regd_no;}
return undef;}
print"\n\n";
####
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\x>cd desktop
C:\Users\x\Desktop>up1.pl
Please ENTER Database Name (.txt): rc.txt
rc.txt Database:
A:50:r1
B:45:r2
C:40:r3
WELCOME to Students' Database:
Enter the Registration Number to view the details: r1
The details of the student are:
Can't call method "display" on an undefined value at C:\Users\x\Desktop
\up1.pl line 45, line 2.
C:\Users\x\Desktop>