http://www.perlmonks.org?node_id=1026832

supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks,

I have written the following script (up1.pl) to view specific data from the database rc.txt. I am getting an error in cmd. I request perlmonks to provide suggestions to correct the error. I am interested to use the script as it appears a bit easier to me. Thanks in advance to the perl monks for suggestions.

The rc.txt is given below:

A:50:r1 B:45:r2 C:40:r3

Here goes the script up1.pl:

########################################## # FINDING DETAILS OF A STUDENT: ##### ########################################## #!/usr/bin/perl use warnings; use strict; ######################## # DATABASE Input: ### ######################## print "\n\n Please ENTER Database Name (.txt): "; my $DNAfilename=<STDIN>; chomp $DNAfilename; # open the file, or exit: unless (open(DNAFILE, $DNAfilename) ) { print "Cannot open file \"$DNAfilename\"\n\n"; exit;} my @DNA= <DNAFILE>; 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 (<DNAFILE>) {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=<STDIN>; 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";

The cmd has shown error:

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\Deskto +p \up1.pl line 45, <STDIN> line 2. C:\Users\x\Desktop>

Replies are listed 'Best First'.
Re: How can one define value to call method "display" at line 45?
by Corion (Patriarch) on Apr 03, 2013 at 11:51 UTC

    Your error happens because</c>

    find_student($reg)

    returns undef, which most likely means that no student matching $reg was found. Consider checking the values you have in your database and whether/how a student can be found given $reg.

Re: How can one define value to call method "display" at line 45?
by Loops (Curate) on Apr 03, 2013 at 11:50 UTC
    What happens if find_student() doesn't find the given student? Undef is returned, which the code then attempts to use as a student object by invoking the display method. You're going to have to check the return value before doing this.
Re: How can one define value to call method "display" at line 45?
by CountZero (Bishop) on Apr 03, 2013 at 22:02 UTC
    Use Data::Dumper to see what your program has actually stored into @students. I think you will be surprised.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: How can one define value to call method "display" at line 45?
by ww (Archbishop) on Apr 03, 2013 at 12:11 UTC
    Suggestion:
    1. Carefully read the error message,lines 21-22 of the cmd output.
    2. search, google, read perls documentation, threads here pertaining to defined and undef
    3. apply knowledge gained in 2 to error in 1
    4. fix the code shown

    If you didn't program your executable by toggling in binary, it wasn't really programming!

Re: How can one define value to call method "display" at line 45?
by Anonymous Monk on Apr 03, 2013 at 11:47 UTC

    Thanks in advance to the perl monks for suggestions.

    Improve the style, perltidy or some such, then you might stand a chance

Re: How can one define value to call method "display" at line 45?
by poj (Abbot) on Apr 03, 2013 at 14:56 UTC
    The new constructor code needs a class parameter like this
    {package Student; sub new { my ($class, $name, $age, $regd_no)=@_; my $objref = { NAME => $name, AGE => $age, REGD_NO => $regd_no }; print"\n class: $class\n"; return bless $objref, $class; } ...
    and the text file needs to be opened again to read the file a second time
    ... my @students; open (DNAFILE, '<', $DNAfilename) or die "Perl says $!"; while (<DNAFILE>) { chomp; push @students, Student->new(split /:/); } ...
    poj

      Hi poj,

      Thanks. Your code has helped me a lot to solve the problem.

      Regards,