Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl code to read a file is?? 1

Status
Not open for further replies.

rjtayl27

Programmer
May 11, 2004
2
0
0
US
The perl code to read a file is???
 
open(FILE,"$filename");
my $file;
while(<FILE>){$file.=$_;}
close(FILE);

# now $file contains the file contents
 
The problem with your question is
that there are many ways to do it.
Example: will list a file.

#-------------------------------------
if (open(Myfile, "<anyfilename"))
{
# show this listing if open succeeds
}
else
{
print "Cannot open Myfile!\n";
exit 1;
}
while(defined($a=<Myfile>))
{
print $a,"\n”;
}
close(Myfile);
exit 0;
#-------------------------------------
 
yet another way...
Code:
use File::Slurp;

my $text = read_file( 'filename' ) ;  #or....
my @lines = read_file( 'filename' ) ;

--jim
 
Another one is a standard-text-in to standard-text-out,
pipe program in one line.

while(defined($a=<STDIN>)) {print $a; } exit 0;

the following command in a batch Windows environment
acts as a standard pipe-filter.

With the proper path in place to find the perl interpreter
you can code

C:\> perl pipe.pl <a:\anytext.txt

to type a file to the screen

C:\> perl pipe.pl <a:\anytext.txt >lpt1:

to type a file to the printer

C:\> perl pipe.pl <a:\anytext.txt | more

to pass the file to another filter like “more” to see one screen at a time.

With a little modification it can be used to create special filters.
 
typical instructor :) never use one word when ten will make it more complex

while(<>){
print
}


Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Your right Mike but the nature of the question indicated a new perl programmer.

by if I used the default &_ I would have been afraid he would use some other statement that modifies it and not understand.

The simplest code can be best up to a point, then I back off with new students. My first response was the better answer for someone that is just wanting to learn to read a file and list it. Then he can do what ever he wants to the records.

I guess that is just the teacher in me but thanks anyway.
 
Your[correction]You're[/correction] right Mike but the nature of the question indicated a new perl programmer.

Teaching sometimes is waiting until someone asks the question in the right way IMHO, and not to overload them with our accumulated knowledge, until such time as they have conetxt

Just my .02c (deprecated) .01c
--Paul
 
if someone posts "The perl code to read a file is???", wouldn't it indicate this person should be pointed to a beginner's tutorial? - possibly by google'ing "perl tutorial
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top