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

hello...

i have recently started learning perl.....and having issues when i try to read input from another file. the details of a script that i tried to run are as follows:

i made a text file by the name "text.txt" and the contents were
Sidra|38|BE Hira|48|BE Shagufta|50|BE
Then i wrote the following script
open(DAT, "text.txt"); $data_file=" text.txt "; open(DAT, $data_file); @raw_data=; close(DAT); foreach $student (@raw_data) { chomp($student); ($name,$roll_no,$class)=split(/\|/,$student); print "The student $name bearing roll number $roll_no is in class $cla +ss"; print " \n"; }
the script produces no output and displays a message saying

"readline () closed filehandle at line "

I tried the same with another file by the name "text.dat" holding the same data but it did not work either. Please help me out resolving this issue. Thankyou...

Replies are listed 'Best First'.
Re: reading input from a file
by Utilitarian (Vicar) on Feb 06, 2010 at 22:26 UTC
    Welcome to the monastery, the preview page explains how to mark up text so that it is formatted correctly, you should use <code> tags around your code like this, I have added a few, hopefully helpful, comments
    use strict; # these two lines will save you use warnings; # endless head scratching open(DAT, "text.txt"); # you open this file again in two lines $data_file="<text.txt "; # three argument version of open is preferabl +e open(DAT, $data_file); # you should check the return value of open @raw_data=<DAT>; # Assumed this was what the line said close(DAT); foreach $student (@raw_data) { chomp($student); ($name,$roll_no,$class)=split(/\|/,$student); print "The student $name bearing roll number $roll_no is in class $cla +ss"; print " \n"; }

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: reading input from a file
by toolic (Bishop) on Feb 06, 2010 at 23:23 UTC
    You should always post the code your are using. Since your code does not even compile, we can not reproduce your error.

    Post your actual code in code tags: see Writeup Formatting Tips.

    use strict and warnings

    From the code you have shown, there seems to be no need to read your whole file into an array; you could read it in line-by-line and process it as you go. That would save memory if your input file is large. UNTESTED:

    use strict; use warnings; my $file = 'text.txt'; open my $fh, '<', $file or die "can not open $file: $!"; while (<$fh>) { chomp; my ($name, $roll_no, $class) = split /\|/; print "The student $name bearing roll number $roll_no is in class +$class\n"; } close $fh;

    If you really do need to read the file into an array, you could slurp that file (and slurp it good!): File::Slurp.

    Updated with code example.

      i have made a user directory by the name of VOIP2..i went into VOIP2 and made 2 dir file2.pl using mkdir file2.pl and mkdir text.txt the perl script you suggested exists in file2.pl ..and now when i run it once i m in file2.pl it gives the following error: cant open text.txt: NO SUCH FILE OR DIRECTORY AT ./FILE2.PL LINE5. could it be because once i m in file2.pl it cant view text.txt?? should i not be using mkdir?? please help!! thanks