Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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>

In reply to How can one define value to call method "display" at line 45? by supriyoch_2008

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    [marto]: good morning all

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (10)
    As of 2018-07-17 08:05 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















      Results (358 votes). Check out past polls.

      Notices?